![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mutex Example
Fork of rtos_mutex by
main.cpp@5:384d6cef11d4, 2017-01-11 (annotated)
- Committer:
- Bartek Szatkowski
- Date:
- Wed Jan 11 12:50:36 2017 +0000
- Revision:
- 5:384d6cef11d4
- Parent:
- 1:0f886ffbe0c1
- Child:
- 6:1ae0d86d2020
Update example to current API
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:0f886ffbe0c1 | 1 | #include "mbed.h" |
emilmont | 1:0f886ffbe0c1 | 2 | #include "rtos.h" |
emilmont | 1:0f886ffbe0c1 | 3 | |
Bartek Szatkowski |
5:384d6cef11d4 | 4 | Mutex stdio_mutex; |
emilmont | 1:0f886ffbe0c1 | 5 | |
emilmont | 1:0f886ffbe0c1 | 6 | void notify(const char* name, int state) { |
emilmont | 1:0f886ffbe0c1 | 7 | stdio_mutex.lock(); |
emilmont | 1:0f886ffbe0c1 | 8 | printf("%s: %d\n\r", name, state); |
emilmont | 1:0f886ffbe0c1 | 9 | stdio_mutex.unlock(); |
emilmont | 1:0f886ffbe0c1 | 10 | } |
emilmont | 1:0f886ffbe0c1 | 11 | |
emilmont | 1:0f886ffbe0c1 | 12 | void test_thread(void const *args) { |
emilmont | 1:0f886ffbe0c1 | 13 | while (true) { |
emilmont | 1:0f886ffbe0c1 | 14 | notify((const char*)args, 0); Thread::wait(1000); |
emilmont | 1:0f886ffbe0c1 | 15 | notify((const char*)args, 1); Thread::wait(1000); |
emilmont | 1:0f886ffbe0c1 | 16 | } |
emilmont | 1:0f886ffbe0c1 | 17 | } |
emilmont | 1:0f886ffbe0c1 | 18 | |
emilmont | 1:0f886ffbe0c1 | 19 | int main() { |
Bartek Szatkowski |
5:384d6cef11d4 | 20 | Thread t2; |
Bartek Szatkowski |
5:384d6cef11d4 | 21 | Thread t3; |
Bartek Szatkowski |
5:384d6cef11d4 | 22 | |
Bartek Szatkowski |
5:384d6cef11d4 | 23 | t2.start(callback(test_thread, (void *)"Th 2")); |
Bartek Szatkowski |
5:384d6cef11d4 | 24 | t3.start(callback(test_thread, (void *)"Th 3")); |
Bartek Szatkowski |
5:384d6cef11d4 | 25 | |
emilmont | 1:0f886ffbe0c1 | 26 | test_thread((void *)"Th 1"); |
emilmont | 1:0f886ffbe0c1 | 27 | } |