![](/media/cache/profiles/af7fc8485f40dc495bc626d6dbe7faf1.50x50_q85.png)
Basic example showing the CMSIS-RTOS Semaphore 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_semaphore
semaphore example
main.cpp@2:66f36aff7050, 2012-11-23 (annotated)
- Committer:
- emilmont
- Date:
- Fri Nov 23 10:53:06 2012 +0000
- Revision:
- 2:66f36aff7050
- Parent:
- 1:7aa2ff92570b
update libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:7aa2ff92570b | 1 | #include "mbed.h" |
emilmont | 1:7aa2ff92570b | 2 | #include "cmsis_os.h" |
emilmont | 1:7aa2ff92570b | 3 | |
emilmont | 1:7aa2ff92570b | 4 | osSemaphoreId two_slots; |
emilmont | 1:7aa2ff92570b | 5 | osSemaphoreDef(two_slots); |
emilmont | 1:7aa2ff92570b | 6 | |
emilmont | 1:7aa2ff92570b | 7 | void test_thread(void const *name) { |
emilmont | 1:7aa2ff92570b | 8 | while (true) { |
emilmont | 1:7aa2ff92570b | 9 | osSemaphoreWait(two_slots, osWaitForever); |
emilmont | 1:7aa2ff92570b | 10 | printf("%s\n\r", (const char*)name); |
emilmont | 1:7aa2ff92570b | 11 | osDelay(1000); |
emilmont | 1:7aa2ff92570b | 12 | osSemaphoreRelease(two_slots); |
emilmont | 1:7aa2ff92570b | 13 | } |
emilmont | 1:7aa2ff92570b | 14 | } |
emilmont | 1:7aa2ff92570b | 15 | |
emilmont | 1:7aa2ff92570b | 16 | void t2(void const *argument) {test_thread("Th 2");} |
emilmont | 1:7aa2ff92570b | 17 | osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE); |
emilmont | 1:7aa2ff92570b | 18 | |
emilmont | 1:7aa2ff92570b | 19 | void t3(void const *argument) {test_thread("Th 3");} |
emilmont | 1:7aa2ff92570b | 20 | osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE); |
emilmont | 1:7aa2ff92570b | 21 | |
emilmont | 1:7aa2ff92570b | 22 | int main (void) { |
emilmont | 1:7aa2ff92570b | 23 | two_slots = osSemaphoreCreate(osSemaphore(two_slots), 2); |
emilmont | 1:7aa2ff92570b | 24 | |
emilmont | 1:7aa2ff92570b | 25 | osThreadCreate(osThread(t2), NULL); |
emilmont | 1:7aa2ff92570b | 26 | osThreadCreate(osThread(t3), NULL); |
emilmont | 1:7aa2ff92570b | 27 | |
emilmont | 1:7aa2ff92570b | 28 | test_thread((void *)"Th 1"); |
emilmont | 1:7aa2ff92570b | 29 | } |