freeRtos信号量的使用
2023-12-14 12:24:37
一.信号量的基本概念?
"give"给出资源,计数值加1;"take"获得资源,计数值减1?
二.创建信号量?
一开始的时候任务1计算,计算完之后信号量里面的计数值增加1,任务2获得信号量,但是任务2里面的计数值为0,就在里面阻塞,等任务1释放give信号量的时候,任务2被唤醒。
static int sum = 0;
static volatile int flagCalcEnd = 0;
static volatile int flagUARTused = 0;
static SemaphoreHandle_t xSemCalc;
void Task1Function(void * param)
{
volatile int i = 0;
while (1)
{
for (i = 0; i < 10000000; i++)
sum++;
//printf("1");
xSemaphoreGive(xSemCalc);
vTaskDelete(NULL);
}
}
void Task2Function(void * param)
{
while (1)
{
//if (flagCalcEnd)
flagCalcEnd = 0;
xSemaphoreTake(xSemCalc,portMAX_DELAY);
flagCalcEnd = 1;
printf("sum = %d\r\n", sum);
}
}
void TaskGenericFunction(void * param)
{
while (1)
{
if (!flagUARTused)
{
flagCalcEnd = 1;
printf("%s\r\n", (char *)param);
flagCalcEnd = 0;
vTaskDelay(1);
}
}
}
/*-----------------------------------------------------------*/
int main( void )
{
TaskHandle_t xHandleTask1;
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("Hello, world!\r\n");
xSemCalc=xSemaphoreCreateCounting(10,0);
xTaskCreate(Task1Function, "Task1", 100, NULL, 1, &xHandleTask1);
xTaskCreate(Task2Function, "Task2", 100, NULL, 1, NULL);
//xTaskCreate(TaskGenericFunction, "Task3", 100, "Task 3 is running", 1, NULL);
//xTaskCreate(TaskGenericFunction, "Task4", 100, "Task 4 is running", 1, NULL);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}
void TaskGenericFunction(void * param)
{
while (1)
{
xSemaphoreTake(xSemUART,portMAX_DELAY);//获得信号量
printf("%s\r\n", (char *)param);
xSemaphoreGive(xSemUART);//释放信号量
vTaskDelay(1);
}
}
/*-----------------------------------------------------------*/
int main( void )
{
TaskHandle_t xHandleTask1;
#ifdef DEBUG
debug();
#endif
prvSetupHardware();
printf("Hello, world!\r\n");
xSemCalc=xSemaphoreCreateCounting(10,0);
xTaskCreate(Task1Function, "Task1", 100, NULL, 1, &xHandleTask1);
xTaskCreate(Task2Function, "Task2", 100, NULL, 1, NULL);
xSemUART=xSemaphoreCreateBinary();
xSemaphoreGive(xSemUART);
xTaskCreate(TaskGenericFunction, "Task3", 100, "Task 3 is running", 1, NULL);
xTaskCreate(TaskGenericFunction, "Task4", 100, "Task 4 is running", 1, NULL);
/* Start the scheduler. */
vTaskStartScheduler();
/* Will only get here if there was not enough heap space to create the
idle task. */
return 0;
}
三.give和take?
?
文章来源:https://blog.csdn.net/lovesjj_1011/article/details/134986933
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!