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 osMutexId stdio_mutex;
00009 osMutexDef(stdio_mutex);
00010 
00011 void notify(const char* name, int state) {
00012     osMutexWait(stdio_mutex, osWaitForever);
00013     printf("%s: %d\n\r", name, state);
00014     osMutexRelease(stdio_mutex);
00015 }
00016 
00017 void test_thread(void const *args) {
00018     while (true) {
00019         notify((const char*)args, 0); osDelay(1000);
00020         notify((const char*)args, 1); osDelay(1000);
00021     }
00022 }
00023 
00024 void t2(void const *argument) {test_thread("Th 2");}
00025 osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE);
00026 
00027 void t3(void const *argument) {test_thread("Th 3");}
00028 osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE);
00029 
00030 int main() {
00031     stdio_mutex = osMutexCreate(osMutex(stdio_mutex));
00032 
00033     osThreadCreate(osThread(t2), NULL);
00034     osThreadCreate(osThread(t3), NULL);
00035 
00036     test_thread((void *)"Th 1");
00037 }