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

Files at this revision

API Documentation at this revision

Comitter:
cspista
Date:
Thu Mar 17 12:56:18 2022 +0000
Commit message:
final version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
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
diff -r 000000000000 -r a1297a50684e mbed-rtos.lib
--- /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
diff -r 000000000000 -r a1297a50684e mbed.bld
--- /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