Mistake on this page?
Report an issue in GitHub or email us

ThisThread

Tip: 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.

Use the ThisThread class to control the current thread.

Unlike the Thread API, which allows you to create, join and end threads, ThisThread lets you control the thread that's currently running. A thread may not have a corresponding Mbed Thread object because you can create a thread directly with CMSIS-RTOS APIs, or it might be main's thread. You can't manipulate those with Thread methods, but ThisThread functions still work from inside them.

ThisThread class reference

ThisThread example

Spawn a thread to blink for 5 seconds before setting a flag to trigger the thread to terminate.

#include "mbed.h"
#include "rtos.h"

Thread thread;
DigitalOut led1(LED1);

#define STOP_FLAG 1

// Blink function toggles the LED in a long running loop
void blink(DigitalOut *led) {
    while (!ThisThread::flags_wait_any_for(STOP_FLAG, 1000)) {
        *led = !*led;
    }
}

// Spawns a thread to run blink for 5 seconds
int main() {
    thread.start(callback(blink, &led1));
    ThisThread::sleep_for(5000);
    thread.flags_set(STOP_FLAG);
    thread.join();
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.