Basic example showing the CMSIS-RTOS mutex API
mbed 2 and mbed OS 5
This is an mbed 2 example. mbed OS 5 has integrated the mbed library with mbed-rtos. For an mbed-os example, please see:
Import programrtos_mutex
mutex example
main.cpp@2:1e00d038091b, 2012-11-23 (annotated)
- Committer:
- emilmont
- Date:
- Fri Nov 23 10:51:24 2012 +0000
- Revision:
- 2:1e00d038091b
- Parent:
- 1:1af1f0df8694
update libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:1af1f0df8694 | 1 | #include "mbed.h" |
emilmont | 1:1af1f0df8694 | 2 | #include "cmsis_os.h" |
emilmont | 1:1af1f0df8694 | 3 | |
emilmont | 1:1af1f0df8694 | 4 | osMutexId stdio_mutex; |
emilmont | 1:1af1f0df8694 | 5 | osMutexDef(stdio_mutex); |
emilmont | 1:1af1f0df8694 | 6 | |
emilmont | 1:1af1f0df8694 | 7 | void notify(const char* name, int state) { |
emilmont | 1:1af1f0df8694 | 8 | osMutexWait(stdio_mutex, osWaitForever); |
emilmont | 1:1af1f0df8694 | 9 | printf("%s: %d\n\r", name, state); |
emilmont | 1:1af1f0df8694 | 10 | osMutexRelease(stdio_mutex); |
emilmont | 1:1af1f0df8694 | 11 | } |
emilmont | 1:1af1f0df8694 | 12 | |
emilmont | 1:1af1f0df8694 | 13 | void test_thread(void const *args) { |
emilmont | 1:1af1f0df8694 | 14 | while (true) { |
emilmont | 1:1af1f0df8694 | 15 | notify((const char*)args, 0); osDelay(1000); |
emilmont | 1:1af1f0df8694 | 16 | notify((const char*)args, 1); osDelay(1000); |
emilmont | 1:1af1f0df8694 | 17 | } |
emilmont | 1:1af1f0df8694 | 18 | } |
emilmont | 1:1af1f0df8694 | 19 | |
emilmont | 1:1af1f0df8694 | 20 | void t2(void const *argument) {test_thread("Th 2");} |
emilmont | 1:1af1f0df8694 | 21 | osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE); |
emilmont | 1:1af1f0df8694 | 22 | |
emilmont | 1:1af1f0df8694 | 23 | void t3(void const *argument) {test_thread("Th 3");} |
emilmont | 1:1af1f0df8694 | 24 | osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE); |
emilmont | 1:1af1f0df8694 | 25 | |
emilmont | 1:1af1f0df8694 | 26 | int main() { |
emilmont | 1:1af1f0df8694 | 27 | stdio_mutex = osMutexCreate(osMutex(stdio_mutex)); |
emilmont | 1:1af1f0df8694 | 28 | |
emilmont | 1:1af1f0df8694 | 29 | osThreadCreate(osThread(t2), NULL); |
emilmont | 1:1af1f0df8694 | 30 | osThreadCreate(osThread(t3), NULL); |
emilmont | 1:1af1f0df8694 | 31 | |
emilmont | 1:1af1f0df8694 | 32 | test_thread((void *)"Th 1"); |
emilmont | 1:1af1f0df8694 | 33 | } |