Thread Sync Example

Dependencies:   C12832

Revision:
1:a477e0d640e0
Parent:
0:567c690c073a
Child:
2:29dca06b0ee0
--- a/main.cpp	Wed Mar 22 15:16:05 2017 +0000
+++ b/main.cpp	Wed Mar 22 15:31:42 2017 +0000
@@ -9,7 +9,7 @@
     DigitalOut _led;
  
 public:
-    Rgb(PinName ld) : _led(ld) { _led = 1};  // Constructor
+    Rgb(PinName ld) : _led(ld) { _led = 1;};  // Constructor
     
     void LedOn() {
         _led = 0;
@@ -120,15 +120,45 @@
    }
 }
 
-Thread thread1;
+DigitalOut Led4(LED4);
+volatile bool running = true;
+
+// Callback function to pass arguments to params
+void blink(DigitalOut *led) {
+    osThreadSetPriority(osThreadGetId(), osPriorityIdle);
+    while (running) {
+        *led = !*led;
+        wait(1);
+    }
+}
+
+// https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/tasks/rtos/#thread
+
+Thread thread1(osPriorityNormal, DEFAULT_STACK_SIZE, NULL);
+// Thread thread1;
 Thread thread2;
 Thread thread3;
+// parametrisierter Thread
+Thread pthread (osPriorityNormal, DEFAULT_STACK_SIZE, NULL);
 
 int main() {
     thread1.start(Led1_Blink);
     thread2.start(Led2_Blink);
-    thread3.start(Led3_Blink);
+    thread3.start(Led3_Blink); 
+    // Start parametrisierter Thread mit Callback Funktion
+    pthread.start(callback(blink, &Led4));
 
+    osThreadSetPriority(osThreadGetId(), osPriorityIdle);   // osPriorityHigh, 
+                                                        // osPriorityIdle
+    Thread::yield();
+    printf("Priority is %i\r\n", thread1.get_priority());
+    Thread::wait(1);
+
+    thread1.join();
+    thread2.join();
+    thread3.join();
+    
     while(1) {
     }
+    thread1.terminate();
 }