![](/media/cache/profiles/pista-2018.jpg.50x50_q85.jpg)
Synchronize two tasks by the help of an event signal. Thread1 (the main() thread) changes the state of LED1 when an event flag arrives. Thread2 sends event flags at reguar intervals. The interesting feature of this demo: how to address thread1 (i.e. the main task)? We use Thread::gettid() to obtain the task ID of the main task.In order to send a signal, we have to use the osSignalSet() function from the underlaying CMSIS RTOS layer. Link: https://developer.mbed.org/questions/1256/How-to-reference-the-main-thread-when-us/
Diff: main.cpp
- Revision:
- 0:a1297a50684e
diff -r 000000000000 -r a1297a50684e main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 17 12:56:18 2022 +0000 @@ -0,0 +1,23 @@ +#include "mbed.h" +#include "rtos.h" + +DigitalOut led(LED1); +osThreadId mainThreadID; + +void signal_thread(void const *argument) { + while (true) { + Thread::wait(1000); + osSignalSet(mainThreadID, 0x1); + } +} + +int main (void) { + mainThreadID = Thread::gettid(); + Thread thread(signal_thread); + + while (true) { + // Signal flags that are reported as event are automatically cleared. + osSignalWait(0x1, osWaitForever); + led = !led; + } +} \ No newline at end of file