RTOS——互斥锁(mutex)

互斥锁的作用:防止多个线程同时修改某个共享资源(全局变量)

优先级反转问题

xTaskCreate(LowPriorityTask, "LowPriorityTask", 4096, NULL, 2, LowPriorityTask_Handle);
xTaskCreate(MidPriorityTask, "MidPriorityTask", 4096, NULL, 3, MidPriorityTask_Handle);
xTaskCreate(HighPriorityTask, "HighPriorityTask", 4096, NULL, 4, HighPriorityTask_Handle);


static void HighPriorityTask(void *pvParameters)
{
	BaseType_t ret = pdPASS;
	while (1)
	{
		ESP_LOGI(TAG, "HigPriorityTask GET");
		ret = xSemaphoreTake(MuxSem_Handle, portMAX_DELAY);
		if (ret == pdPASS)
			ESP_LOGI(TAG, "HigPriorityTask runing");
		ESP_LOGI(TAG, "HigPriorityTask release");
		xSemaphoreGive(MuxSem_Handle);
		vTaskDelay(1000 / portTICK_RATE_MS);
	}
}

static void MidPriorityTask(void *pvParameters)
{
	while (1)
	{
		ESP_LOGI(TAG, "MidPriorityTask runing");
		vTaskDelay(1000 / portTICK_RATE_MS);
	}
}

static void LowPriorityTask(void *pvParameters)
{
	BaseType_t ret = pdPASS;	
	while (1)
	{
		ret = xSemaphoreTake(MuxSem_Handle, portMAX_DELAY);
		if (ret == pdPASS)
		{
			ESP_LOGI(TAG, "LowPriorityTask Get");
		}
		// HighPriorityTask 如果获取互斥锁将会被阻塞
		// MidPriorityTask 可以抢占LowPriorityTask
		vTaskDelay(2000 / portTICK_RATE_MS);
		ESP_LOGI(TAG, "LowPriorityTask Release");
		ret = xSemaphoreGive(MuxSem_Handle);
		led_toggle();
		vTaskDelay(1000 / portTICK_RATE_MS);
	}
}
[0;32mI (238) rtos mutex: mutex start...[0m
[0;32mI (239) rtos mutex: MuxSem_Handle OK[0m
[0;32mI (240) rtos mutex: LowPriorityTask OK[0m
[0;32mI (242) rtos mutex: MidPriorityTask OK[0m
[0;32mI (248) rtos mutex: HighPriorityTask OK[0m
[0;32mI (255) rtos mutex: HigPriorityTask GET[0m
[0;32mI (262) rtos mutex: HigPriorityTask runing[0m
[0;32mI (269) rtos mutex: HigPriorityTask release[0m
[0;32mI (276) rtos mutex: MidPriorityTask runing[0m
[0;32mI (284) rtos mutex: LowPriorityTask Get[0m
[0;32mI (1275) rtos mutex: HigPriorityTask GET[0m
[0;32mI (1276) rtos mutex: MidPriorityTask runing[0m
[0;32mI (2276) rtos mutex: MidPriorityTask runing[0m
[0;32mI (2286) rtos mutex: LowPriorityTask Release[0m
[0;32mI (2287) rtos mutex: HigPriorityTask runing[0m
[0;32mI (2288) rtos mutex: HigPriorityTask release[0m
[0;32mI (3276) rtos mutex: MidPriorityTask runing[0m
[0;32mI (3286) rtos mutex: HigPriorityTask GET[0m
[0;32mI (3287) rtos mutex: HigPriorityTask runing[0m
[0;32mI (3288) rtos mutex: HigPriorityTask release[0m
[0;32mI (3292) rtos mutex: LowPriorityTask Get[0m
[0;32mI (4276) rtos mutex: MidPriorityTask runing[0m
[0;32mI (4286) rtos mutex: HigPriorityTask GET[0m
[0;32mI (5276) rtos mutex: MidPriorityTask runing[0m
[0;32mI (5296) rtos mutex: LowPriorityTask Release[0m
[0;32mI (5297) rtos mutex: HigPriorityTask runing[0m

RTOS——互斥锁(mutex)

上一篇:18523-47-2,3-Azidopropanoic acid,Propanoic acid- 3-azido叠氮丙酸可在活化剂(如EDC或DCC)存在下与伯胺基反应,形成稳定的酰胺键


下一篇:*** RuntimeError: CUDA error: out of memory.