![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Queues
Dependencies: ELEC350-Practicals-FZ429
Fork of Task631-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); }