Case study Embedded System Design / Mbed 2 deprecated Case_Study_rtos_semaphore

Dependencies:   mbed-rtos mbed

Fork of rtos_semaphore by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 
00004 Semaphore two_slots(2);                         //Setup the use of two semaphores
00005 
00006 void test_thread(void const *name) {            //setup thread function 
00007     while (true) {
00008         two_slots.wait();                       //Wait to get a semaphore to use the 
00009         printf("%s\n\r", (const char*)name);    //when have semaphore printf on serial of thread number
00010         Thread::wait(1000);                     //thread wait
00011         two_slots.release();                    //Release semaphore back to RTOS
00012     }
00013 }
00014 
00015 int main (void) {
00016     Thread t2(test_thread, (void *)"Th 2");     //start thread
00017     Thread t3(test_thread, (void *)"Th 3");     //start thread
00018     
00019     test_thread((void *)"Th 1");                //function call in main thread
00020 }