ThisThread
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();
}
Related content
- Thread API reference.