Mutex Example

Dependencies:   mbed-rtos mbed

Fork of rtos_mutex by mbed official

Revision:
7:ff2e3fbefea7
Parent:
6:1ae0d86d2020
--- a/main.cpp	Fri Jan 13 19:49:29 2017 +0000
+++ b/main.cpp	Fri Jan 20 11:51:11 2017 +0000
@@ -1,25 +1,54 @@
 #include "mbed.h"
+#include "rtos.h"
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+ 
+Mutex led_mutex; 
 
-Mutex stdio_mutex;
-Thread t2;
-Thread t3;
-    
-void notify(const char* name, int state) {
-    stdio_mutex.lock();
-    printf("%s: %d\n\r", name, state);
-    stdio_mutex.unlock();
+void delay(void){
+for(int i=0;i<65000;i++){
+    for(int j=0;j<100;j++)
+    {
+        }
+ }
 }
-
-void test_thread(void const *args) {
+ 
+void test_thread1(void const *args) {
     while (true) {
-        notify((const char*)args, 0); wait(1);
-        notify((const char*)args, 1); wait(1);
+        led_mutex.lock();
+        printf("thd1 toogling\n");
+        led1 = !led1;
+        delay();
+        led_mutex.unlock();
+        Thread::wait(1000);
     }
 }
 
+void test_thread2(void const *args) {
+    while (true) {
+        led_mutex.lock();
+        printf("thd2 toogling\n");
+        led1 = !led1;
+        delay();
+        led_mutex.unlock();
+        Thread::wait(1000);
+    }
+}
+void test_thread(void const *args) {
+    while (true) {
+        led_mutex.lock();
+        printf("main toogling\n");
+        led1 = !led1;
+        delay();
+        led_mutex.unlock();
+        Thread::wait(1000);
+    }
+}
+ 
 int main() {
-    t2.start(callback(test_thread, (void *)"Th 2"));
-    t3.start(callback(test_thread, (void *)"Th 3"));
-
-    test_thread((void *)"Th 1");
-}
+    Thread t2(test_thread1, (void *)"Th 1");
+    Thread t3(test_thread2, (void *)"Th 2");
+    
+    test_thread((void *)"main");
+}
\ No newline at end of file