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/

Dependencies:   mbed mbed-rtos

Committer:
cspista
Date:
Thu Mar 17 12:56:18 2022 +0000
Revision:
0:a1297a50684e
final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cspista 0:a1297a50684e 1 #include "mbed.h"
cspista 0:a1297a50684e 2 #include "rtos.h"
cspista 0:a1297a50684e 3
cspista 0:a1297a50684e 4 DigitalOut led(LED1);
cspista 0:a1297a50684e 5 osThreadId mainThreadID;
cspista 0:a1297a50684e 6
cspista 0:a1297a50684e 7 void signal_thread(void const *argument) {
cspista 0:a1297a50684e 8 while (true) {
cspista 0:a1297a50684e 9 Thread::wait(1000);
cspista 0:a1297a50684e 10 osSignalSet(mainThreadID, 0x1);
cspista 0:a1297a50684e 11 }
cspista 0:a1297a50684e 12 }
cspista 0:a1297a50684e 13
cspista 0:a1297a50684e 14 int main (void) {
cspista 0:a1297a50684e 15 mainThreadID = Thread::gettid();
cspista 0:a1297a50684e 16 Thread thread(signal_thread);
cspista 0:a1297a50684e 17
cspista 0:a1297a50684e 18 while (true) {
cspista 0:a1297a50684e 19 // Signal flags that are reported as event are automatically cleared.
cspista 0:a1297a50684e 20 osSignalWait(0x1, osWaitForever);
cspista 0:a1297a50684e 21 led = !led;
cspista 0:a1297a50684e 22 }
cspista 0:a1297a50684e 23 }