Wait
Note: wait
is deprecated in favor of explicit sleep functions. To sleep, replace wait
with ThisThread::sleep_for
(C++) or thread_sleep_for
(C). To wait (without sleeping), call wait_us
. wait_us
is safe to call from ISR context.
Wait functions provide simple wait capabilities. The OS scheduler puts the current thread in waiting state
, allowing another thread to execute. Even better: If there are no other threads in ready state
, it can put the whole microcontroller to sleep
, saving energy.
Avoiding OS delay
When you call wait, your board's CPU will sleep in the RTOS for the whole number of milliseconds and then spin as necessary to make up the remaining fraction of a millisecond. However, it blocks the platform deep sleep for the entire duration.
Wait function reference
Example
#include "mbed.h"
DigitalOut heartbeat(LED1);
int main() {
while (1) {
heartbeat = 1;
wait(0.5);
heartbeat = 0;
wait(0.5);
}
}
Related content
- RTOS overview.
- Application flow control tutorial.