finished

Dependencies:   mbed-rtos mbed

Fork of rtos_mutex by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"                                           //Include the mbed.h file
00002 #include "rtos.h"                                           //Include the rtos.h file
00003 
00004 Mutex stdio_mutex;                                          //Setup the mutex
00005 
00006 void notify(const char* name, int state) {                  //function and passes data NAME and STATE to the function
00007     stdio_mutex.lock();                                     //mutex lock stdio_mutex 
00008     printf("%s: %d\n\r", name, state);                      //read name and state and print them on serial
00009     stdio_mutex.unlock();                                   //mutex unlock stdio_mutex 
00010 }
00011 
00012 void test_thread(void const *args) {                        //Thread and function, pass data to the thread/function
00013     while (true) {                                          //Super loop
00014         notify((const char*)args, 0);                       //function call, passes number and toggles data to the function
00015         Thread::wait(1000);                                 //Thread wait 1 sec
00016         notify((const char*)args, 1);                       ////function call, pass two data to the function
00017         Thread::wait(1000);                                 //Thread wait 1 sec
00018     }                                                       //end super loop
00019 }                                                           //end function
00020 
00021 int main() {
00022     Thread t2(test_thread, (void *)"Th 2");                 //start thread t2
00023     Thread t3(test_thread, (void *)"Th 3");                 //start thread t3
00024     
00025     test_thread((void *)"Th 1");                            //function call
00026 }                                                           //end main