互斥量(Mutex)
互斥量又称互斥信号量(本质也是一种信号量,不具备传递数据功能),是一种特殊的二值信号量,它和信号量不同的是,它支持互斥量所有权、递归访问以及防止优先级翻转的特性。比如有两个任务,A在运行,B就运行不了。
实验:创建三个任务,从高到低设置优先级,在任务一和三中使用互斥量,观察执行顺序。
实现:在【STM32】FreeRTOS消息队列和信号量学习基础上进行修改
void MX_FREERTOS_Init(void) {/* USER CODE BEGIN Init *//* USER CODE END Init *//* Create the mutex(es) *//* definition and creation of Mutex01 */osMutexDef(Mutex01);Mutex01Handle = osMutexCreate(osMutex(Mutex01));/* USER CODE BEGIN RTOS_MUTEX *//* add mutexes, ... *//* USER CODE END RTOS_MUTEX *//* Create the semaphores(s) *//* USER CODE BEGIN RTOS_SEMAPHORES *//* add semaphores, ... *//* USER CODE END RTOS_SEMAPHORES *//* USER CODE BEGIN RTOS_TIMERS *//* start timers, add new ones, ... *//* USER CODE END RTOS_TIMERS *//* USER CODE BEGIN RTOS_QUEUES *//* add queues, ... *//* USER CODE END RTOS_QUEUES *//* Create the thread(s) *//* definition and creation of Task1 */osThreadDef(Task1, StartDefaultTask, osPriorityHigh, 0, 128);Task1Handle = osThreadCreate(osThread(Task1), NULL);/* definition and creation of Task2 */osThreadDef(Task2, StartTask02, osPriorityAboveNormal, 0, 128);Task2Handle = osThreadCreate(osThread(Task2), NULL);/* definition and creation of Task3 */osThreadDef(Task3, StartTask03, osPriorityNormal, 0, 128);Task3Handle = osThreadCreate(osThread(Task3), NULL);/* USER CODE BEGIN RTOS_THREADS *//* add threads, ... *//* USER CODE END RTOS_THREADS */}/* USER CODE BEGIN Header_StartDefaultTask */
/*** @brief Function implementing the Task1 thread.* @param argument: Not used* @retval None*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{/* USER CODE BEGIN StartDefaultTask *//* Infinite loop */for(;;){osDelay(10);//堵塞if(xSemaphoreTake(Mutex01Handle,portMAX_DELAY)!=pdTRUE){printf("task1,进不去\r\n");}else{printf("task1,抢占进入\r\n");}xSemaphoreGive(Mutex01Handle);printf("task1,完成\r\n");}/* USER CODE END StartDefaultTask */
}/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the Task2 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void const * argument)
{/* USER CODE BEGIN StartTask02 *//* Infinite loop */for(;;){osDelay(2);//堵塞printf("task2,运行\r\n");}/* USER CODE END StartTask02 */
}/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the Task3 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void StartTask03(void const * argument)
{/* USER CODE BEGIN StartTask03 *//* Infinite loop */for(;;){xSemaphoreTake(Mutex01Handle,0);printf("task3,进入\r\n");xSemaphoreGive(Mutex01Handle);printf("task3,完成\r\n");}/* USER CODE END StartTask03 */
}
现象:
最低优先级先执行,最高优先级其次执行,最后是中间优先级执行。