#include "FreeRTOS.h" #include "task.h" #include "semphr.h" TaskHandle_t th1,th2,th3; SemaphoreHandle_t sh; int x; void task_1(void* p) { for ( ;; ) { if(xSemaphoreTake(sh,500)==0) { printf("Task 1 Failed to take mutex within 500ms\n"); } else { x=1; printf("Task 1 took mutex and x=%u\n",x); } xSemaphoreGive(sh); vTaskDelay(1000); } } void task_2(void* p) { for ( ;; ) { if(xSemaphoreTake(sh,500)==0) { printf("Task 2 Failed to take mutex within 500ms\n"); } else { x=2; printf("Task 2 took mutex and x=%u\n",x); } xSemaphoreGive(sh); vTaskDelay(1000); } } void task_3(void* p) { for ( ;; ) { if(xSemaphoreTake(sh,500)==0) { printf("Task 3 Failed to take mutex within 500ms\n"); } else { x=3; printf("Task 3 took mutex and x=%u\n",x); } xSemaphoreGive(sh); vTaskDelay(1000); } } int Example3(void) { sh=xSemaphoreCreateMutex(); xTaskCreate(task_1, "Task 1", 1024, NULL, 1, &th1); xTaskCreate(task_2, "Task 2", 1024, NULL, 2, &th2); xTaskCreate(task_3, "Task 3", 1024, NULL, 3, &th3); vTaskStartScheduler(); for ( ; ; ); }