![](/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/
Revision 0:a1297a50684e, committed 2022-03-17
- Comitter:
- cspista
- Date:
- Thu Mar 17 12:56:18 2022 +0000
- Commit message:
- final version
Changed in this revision
--- /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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Mar 17 12:56:18 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed-rtos/#5713cbbdb706
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Mar 17 12:56:18 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file