高级会员

- 积分
- 566
- 金钱
- 566
- 注册时间
- 2016-9-28
- 在线时间
- 158 小时
|
1. 声明变量
xSemaphoreHandle xSemaphore;
2. 创建信号量句柄
static void AppTask_xQueueObject (void)
{
/* 创建互斥信号量 */
xSemaphore = xSemaphoreCreateMutex();
if( xSemaphore != NULL )
{
printf(" Create xSemaphore: sucess \r\n");
}
}
3. 在task中应用互斥信号量,保护执行语句
void vTask1( void *pvParameters )
{
while(1)
{
xSemaphoreTake( xSemaphore, portMAX_DELAY);
printf(" Task1 is running \r\n");
xSemaphoreGive( xSemaphore );
}
}
void vTask2( void *pvParameters )
{
portTickType xLastWakeTime;
const portTickType xFrequency = 100;
/* 获取系统当前时间 */
xLastWakeTime = xTaskGetTickCount();
while(1)
{
vTaskDelayUntil( &xLastWakeTime, xFrequency );
xSemaphoreTake( xSemaphore, portMAX_DELAY);
printf(" Task2 is running \r\n");
xSemaphoreGive( xSemaphore );
}
}
|
|