Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "cmsis_os.h"
00003 
00004 #if defined(MBED_RTOS_SINGLE_THREAD)
00005   #error [NOT_SUPPORTED] test not supported
00006 #endif
00007 
00008 osSemaphoreId two_slots;
00009 osSemaphoreDef(two_slots);
00010 
00011 void test_thread(void const *name) {
00012     while (true) {
00013         osSemaphoreWait(two_slots, osWaitForever);
00014         printf("%s\n\r", (const char*)name);
00015         osDelay(1000);
00016         osSemaphoreRelease(two_slots);
00017     }
00018 }
00019 
00020 void t2(void const *argument) {test_thread("Th 2");}
00021 osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE);
00022 
00023 void t3(void const *argument) {test_thread("Th 3");}
00024 osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE);
00025 
00026 int main (void) {
00027     two_slots = osSemaphoreCreate(osSemaphore(two_slots), 2);
00028 
00029     osThreadCreate(osThread(t2), NULL);
00030     osThreadCreate(osThread(t3), NULL);
00031 
00032     test_thread((void *)"Th 1");
00033 }