main.cpp 827 B

123456789101112131415161718192021222324252627282930313233
  1. #include "mbed.h"
  2. #include "cmsis_os.h"
  3. osMutexId stdio_mutex;
  4. osMutexDef(stdio_mutex);
  5. void notify(const char* name, int state) {
  6. osMutexWait(stdio_mutex, osWaitForever);
  7. printf("%s: %d\n\r", name, state);
  8. osMutexRelease(stdio_mutex);
  9. }
  10. void test_thread(void const *args) {
  11. while (true) {
  12. notify((const char*)args, 0); osDelay(1000);
  13. notify((const char*)args, 1); osDelay(1000);
  14. }
  15. }
  16. void t2(void const *argument) {test_thread("Th 2");}
  17. osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE);
  18. void t3(void const *argument) {test_thread("Th 3");}
  19. osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE);
  20. int main() {
  21. stdio_mutex = osMutexCreate(osMutex(stdio_mutex));
  22. osThreadCreate(osThread(t2), NULL);
  23. osThreadCreate(osThread(t3), NULL);
  24. test_thread((void *)"Th 1");
  25. }