mbed_example
/
ConditionVariable_Example_1
Example use of the ConditionVariable class.
Revision 0:36d0e9449606, committed 2018-01-04
- Comitter:
- c1728p9
- Date:
- Thu Jan 04 01:09:36 2018 +0000
- Commit message:
- Initial Commit
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed-os.lib | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 36d0e9449606 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jan 04 01:09:36 2018 +0000 @@ -0,0 +1,48 @@ +#include "mbed.h" + +Mutex mutex; +ConditionVariable cond(mutex); + +// These variables are protected by locking mutex +uint32_t count = 0; +bool done = false; + +void worker_thread() +{ + mutex.lock(); + do { + printf("Worker: Count %lu\r\n", count); + + // Wait for a condition to change + cond.wait(); + + } while (!done); + printf("Worker: Exiting\r\n"); + mutex.unlock(); +} + +int main() { + Thread thread; + thread.start(worker_thread); + + for (int i = 0; i < 5; i++) { + + mutex.lock(); + // Change count and signal this + count++; + printf("Main: Set count to %lu\r\n", count); + cond.notify_all(); + mutex.unlock(); + + wait(1.0); + } + + mutex.lock(); + // Change done and signal this + done = true; + printf("Main: Set done\r\n"); + cond.notify_all(); + mutex.unlock(); + + thread.join(); +} \ No newline at end of file
diff -r 000000000000 -r 36d0e9449606 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Jan 04 01:09:36 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#eca67ca7dafab4ef70c21e2463b541132d0dd691