![](/media/cache/group/mbed-logo-square-512_3.png.50x50_q85.png)
semaphore example
main.cpp@7:755dee0042c3, 2018-02-02 (annotated)
- Committer:
- Jennifer Plunkett
- Date:
- Fri Feb 02 15:14:15 2018 -0600
- Revision:
- 7:755dee0042c3
- Parent:
- 5:574f47121e8e
Fixed bug that made printf() unprotected
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:bdf73f017a77 | 1 | #include "mbed.h" |
emilmont | 1:bdf73f017a77 | 2 | |
Jennifer Plunkett
7:755dee0042c3
|
3
|
Semaphore one_slot(1);
|
|
mab5449 | 5:574f47121e8e | 4 | Thread t2; |
mab5449 | 5:574f47121e8e | 5 | Thread t3; |
Jennifer Plunkett
7:755dee0042c3
|
6
|
|
|
emilmont | 1:bdf73f017a77 | 7 | void test_thread(void const *name) { |
emilmont | 1:bdf73f017a77 | 8 | while (true) { |
Jennifer Plunkett
7:755dee0042c3
|
9
|
one_slot.wait();
|
|
emilmont | 1:bdf73f017a77 | 10 | printf("%s\n\r", (const char*)name); |
mab5449 | 5:574f47121e8e | 11 | wait(1); |
Jennifer Plunkett
7:755dee0042c3
|
12
|
one_slot.release();
|
|
emilmont | 1:bdf73f017a77 | 13 | } |
emilmont | 1:bdf73f017a77 | 14 | } |
emilmont | 1:bdf73f017a77 | 15 | |
emilmont | 1:bdf73f017a77 | 16 | int main (void) { |
Bartek Szatkowski |
4:ec4791bc6554 | 17 | t2.start(callback(test_thread, (void *)"Th 2")); |
Bartek Szatkowski |
4:ec4791bc6554 | 18 | t3.start(callback(test_thread, (void *)"Th 3")); |
Bartek Szatkowski |
4:ec4791bc6554 | 19 | |
emilmont | 1:bdf73f017a77 | 20 | test_thread((void *)"Th 1"); |
emilmont | 1:bdf73f017a77 | 21 | } |