In this example three semaphores are used to mantain the monotonic execution order of three threads. Each task is consumer of a semaphore and at the same time producer of an another semaphore.Watching the RGB LED you can see the order of execution.

Dependencies:   mbed-rtos mbed

Committer:
icserny
Date:
Wed Feb 10 12:40:31 2016 +0000
Revision:
0:61ad4bb19246
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icserny 0:61ad4bb19246 1 /** 10_rtos_3semaphores
icserny 0:61ad4bb19246 2 * In this example three semaphores are used to mantain the
icserny 0:61ad4bb19246 3 * monotonic execution order of three threads.
icserny 0:61ad4bb19246 4 * Each task is consumer of a semaphore and at the same time
icserny 0:61ad4bb19246 5 * producer of an another semaphore.
icserny 0:61ad4bb19246 6 * Watching the RGB LED you can see the order of execution.
icserny 0:61ad4bb19246 7 *
icserny 0:61ad4bb19246 8 * Hardware requirements:
icserny 0:61ad4bb19246 9 * - FRDM-KL25Z board
icserny 0:61ad4bb19246 10 */
icserny 0:61ad4bb19246 11
icserny 0:61ad4bb19246 12 #include "mbed.h"
icserny 0:61ad4bb19246 13 #include "rtos.h"
icserny 0:61ad4bb19246 14
icserny 0:61ad4bb19246 15 Semaphore s1(1); //allow task1 to run
icserny 0:61ad4bb19246 16 Semaphore s2(0); //s2 has no token at start
icserny 0:61ad4bb19246 17 Semaphore s3(0); //s3 has no token at start
icserny 0:61ad4bb19246 18 DigitalOut led1(LED1); //Red LED
icserny 0:61ad4bb19246 19 DigitalOut led2(LED2); //Green LED
icserny 0:61ad4bb19246 20 DigitalOut led3(LED3); //Blue LED
icserny 0:61ad4bb19246 21
icserny 0:61ad4bb19246 22 void thread1(void const* args) {
icserny 0:61ad4bb19246 23 while (true) {
icserny 0:61ad4bb19246 24 s1.wait();
icserny 0:61ad4bb19246 25 led1 = 0; //Red LED ON
icserny 0:61ad4bb19246 26 Thread::wait(500+rand()%500);
icserny 0:61ad4bb19246 27 led1 = 1; //Red LED OFF
icserny 0:61ad4bb19246 28 s2.release();
icserny 0:61ad4bb19246 29 }
icserny 0:61ad4bb19246 30 }
icserny 0:61ad4bb19246 31
icserny 0:61ad4bb19246 32 void thread2(void const* args) {
icserny 0:61ad4bb19246 33 while (true) {
icserny 0:61ad4bb19246 34 s2.wait();
icserny 0:61ad4bb19246 35 led2 = 0; //Green LED ON
icserny 0:61ad4bb19246 36 Thread::wait(500+rand()%500);
icserny 0:61ad4bb19246 37 led2 = 1; //Green LED OFF
icserny 0:61ad4bb19246 38 s3.release();
icserny 0:61ad4bb19246 39 }
icserny 0:61ad4bb19246 40 }
icserny 0:61ad4bb19246 41
icserny 0:61ad4bb19246 42 void thread3(void const* args) {
icserny 0:61ad4bb19246 43 while (true) {
icserny 0:61ad4bb19246 44 s3.wait();
icserny 0:61ad4bb19246 45 led3 = 0; //Blue LED ON
icserny 0:61ad4bb19246 46 Thread::wait(500+rand()%500);
icserny 0:61ad4bb19246 47 led3 = 1; //Blue LED OFF
icserny 0:61ad4bb19246 48 s1.release();
icserny 0:61ad4bb19246 49 }
icserny 0:61ad4bb19246 50 }
icserny 0:61ad4bb19246 51
icserny 0:61ad4bb19246 52 int main (void) {
icserny 0:61ad4bb19246 53 led1 = 1; led2 = 1; led3 = 1; //Switch off all LEDs
icserny 0:61ad4bb19246 54 Thread t2(thread2); //Define and run thread2
icserny 0:61ad4bb19246 55 Thread t3(thread3); //Define and run thread3
icserny 0:61ad4bb19246 56 thread1(NULL); //Run thread1
icserny 0:61ad4bb19246 57 }