633 solution updated for mbed os 54

Fork of Task633Solution-mbedos54 by Stage-1 Students SoCEM

Revision:
1:4fb27aea76b2
Parent:
0:f916cefba2f4
Child:
2:70084af839d3
--- a/main.cpp	Tue Mar 08 11:43:39 2016 +0000
+++ b/main.cpp	Tue Mar 08 12:26:28 2016 +0000
@@ -20,9 +20,8 @@
 DigitalIn  SW1(D4);
 DigitalIn  SW2(D3);
 
-//Thread object references
-Thread* t1;
-Thread* t2;
+//MUTEX Lock
+Mutex *countLock;
 
 //Thread ID for the Main function (CMSIS API)
 osThreadId tidMain;
@@ -36,6 +35,10 @@
     redLED = 1;
     
     for (unsigned int n=0; n<10000; n++) {
+        //Take lock
+        countLock->lock();
+        
+        //Critical section(s)
         count++;
         count++;        
         count++;
@@ -46,6 +49,9 @@
         count++;
         count++;
         count++;   
+        
+        //Release lock
+        countLock->unlock();
     }
     
     redLED = 0;
@@ -57,6 +63,10 @@
     yellowLED = 1;
     
     for (unsigned int n=0; n<10000; n++) {
+        //Take lock
+        countLock->lock();
+        
+        //Critical section(s)        
         count--;
         count--;        
         count--;
@@ -66,7 +76,10 @@
         count--;
         count--;
         count--;
-        count--;               
+        count--;    
+        
+        //Release lock
+        countLock->unlock();                   
     }   
     
     osSignalSet(tidMain, YELLOW_DONE); //Signal main thread we are done
@@ -83,6 +96,9 @@
     //Main thread ID
     tidMain = Thread::gettid();  
     
+    //Create lock
+    countLock = new Mutex();
+    
     //Press the switch to run concurrently
     if (onBoardSwitch == 1) {
         printf("Running sequntially\n");
@@ -102,5 +118,9 @@
     if (count == 0) {
         greenLED = 0;
     }
+    
+    //Tidy
+    delete countLock;
+    
     while(true);
 }