Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task632-mbedos54 by
Diff: main.cpp
- Revision:
- 1:4fb27aea76b2
- Parent:
- 0:f916cefba2f4
- Child:
- 2:70084af839d3
diff -r f916cefba2f4 -r 4fb27aea76b2 main.cpp
--- 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);
}