信号量
二值信号量
SemaphoreHandle_t sem_handle; sem_handle = xSemaphoreCreateBinary(); static void send_Task(void* pvParameters) { while(1) { xSemaphoreGive(sem_handle); vTaskDelay(1000); } } static void receive_Task(void* pvParameters) { while (1) { xSemaphoreTake(sem_handle, portMAX_DELAY); printf("接收二值信号量\r\n"); } }
计数信号量
SemaphoreHandle_t sem_count_handle; sem_count_handle = xSemaphoreCreateCounting(5, 5); static void send_Task(void* pvParameters) { while(1) { vTaskDelay(1000); xSemaphoreGive(sem_count_handle); } } static void receive_Task(void* pvParameters) { while (1) { xSemaphoreTake(sem_count_handle, portMAX_DELAY); printf("接收计数信号量\r\n"); } }