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-rtos mbed

Committer:
icserny
Date:
Tue Feb 23 13:21:25 2016 +0000
Revision:
0:0bbb855fceaf
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icserny 0:0bbb855fceaf 1 /** 10_rtos_signals_to_main
icserny 0:0bbb855fceaf 2 *
icserny 0:0bbb855fceaf 3 * Synchronize two tasks by the help of an event signal.
icserny 0:0bbb855fceaf 4 * Thread1 (the main() thread) changes the state of LED1 when an event flag arrives.
icserny 0:0bbb855fceaf 5 * Thread2 sends event flags at reguar intervals.
icserny 0:0bbb855fceaf 6 * The main problem is here: how to address thread1?
icserny 0:0bbb855fceaf 7 * We use Thread::gettid() to obtain the task ID of the main task.
icserny 0:0bbb855fceaf 8 * In order to send a signal, we have to use the osSignalSet()
icserny 0:0bbb855fceaf 9 * function from the underlaying CMSIS RTOS layer.
icserny 0:0bbb855fceaf 10 * Source: https://developer.mbed.org/questions/1256/How-to-reference-the-main-thread-when-us/
icserny 0:0bbb855fceaf 11 */
icserny 0:0bbb855fceaf 12
icserny 0:0bbb855fceaf 13 #include "mbed.h"
icserny 0:0bbb855fceaf 14 #include "rtos.h"
icserny 0:0bbb855fceaf 15
icserny 0:0bbb855fceaf 16 DigitalOut led(LED1);
icserny 0:0bbb855fceaf 17 osThreadId mainThreadID;
icserny 0:0bbb855fceaf 18
icserny 0:0bbb855fceaf 19 void signal_thread(void const *argument) {
icserny 0:0bbb855fceaf 20 while (true) {
icserny 0:0bbb855fceaf 21 Thread::wait(1000);
icserny 0:0bbb855fceaf 22 osSignalSet(mainThreadID, 0x1);
icserny 0:0bbb855fceaf 23 }
icserny 0:0bbb855fceaf 24 }
icserny 0:0bbb855fceaf 25
icserny 0:0bbb855fceaf 26 int main (void) {
icserny 0:0bbb855fceaf 27 mainThreadID = Thread::gettid();
icserny 0:0bbb855fceaf 28 Thread thread(signal_thread);
icserny 0:0bbb855fceaf 29
icserny 0:0bbb855fceaf 30 while (true) {
icserny 0:0bbb855fceaf 31 // Signal flags that are reported as event are automatically cleared.
icserny 0:0bbb855fceaf 32 osSignalWait(0x1, osWaitForever);
icserny 0:0bbb855fceaf 33 led = !led;
icserny 0:0bbb855fceaf 34 }
icserny 0:0bbb855fceaf 35 }