freeRtos同步互斥与通信

一.简介

二.同步的例子:有缺陷

static int sum=0;
static volatile int flagCaleEnd=0;
void Task1Function(void * param)
{
	volatile int i=0;
	while (1)
	{
		for(i<0;i<10000000;i++)
		{
			sum++;
		}
		flagCaleEnd=1;
		printf("1");
		vTaskDelete(NULL);//自杀
	}
}

void Task2Function(void * param)
{
	while (1)
	{
		if(flagCaleEnd==1){
			printf("sum=%d\r\n",sum);
		}
	}
}


/*-----------------------------------------------------------*/

int main( void )
{
	TaskHandle_t xHandleTask1;
		
#ifdef DEBUG
  debug();
#endif

	prvSetupHardware();

	printf("Hello, world!\r\n");

	xTaskCreate(Task1Function, "Task1", 100, NULL, 1, &xHandleTask1);
	xTaskCreate(Task2Function, "Task2", 100, NULL, 1, NULL);

	/* Start the scheduler. */
	vTaskStartScheduler();

	/* Will only get here if there was not enough heap space to create the
	idle task. */
	return 0;
}

单独执行一个任务可能只需要2s中,两个任务竞争可能需要4s的时间,需要让任务2在任务1运行期间进入Blocked状态,让出CPU资源才能提高效率。 

三.互斥的例子:有缺陷

static int sum=0;
static volatile int flagCaleEnd=0;
void Task1Function(void * param)
{
	volatile int i=0;
	while (1)
	{
		for(i<0;i<10000000;i++)
		{
			sum++;
		}
		flagCaleEnd=1;
		printf("1");
		vTaskDelete(NULL);//自杀
	}
}

void Task2Function(void * param)
{
	while (1)
	{
		if(flagCaleEnd==1){
			printf("sum=%d\r\n",sum);
		}
	}
}

void TaskGenericFunction(void * param)
{
	while (1)
	{
		printf("%s\r\n",(char*)param);
	}
}

/*-----------------------------------------------------------*/

int main( void )
{
	TaskHandle_t xHandleTask1;
		
#ifdef DEBUG
  debug();
#endif

	prvSetupHardware();

	printf("Hello, world!\r\n");

	xTaskCreate(Task1Function, "Task1", 100, NULL, 1, &xHandleTask1);
	//xTaskCreate(Task2Function, "Task2", 100, NULL, 1, NULL);
	
	xTaskCreate(TaskGenericFunction, "Task3", 100, "Task3 is running", 1, NULL);
	xTaskCreate(TaskGenericFunction, "Task4", 100, "Task4 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;
}

任务3和任务4打印的信息参杂在一起并没有完整打印出一个,没有互斥的独占CPU打印出一个完整的。

修改一下加入一个标志位

static int sum=0;
static volatile int flagCaleEnd=0;
static volatile int flagUartUsed=0;

void Task1Function(void * param)
{
	volatile int i=0;
	while (1)
	{
		for(i=0;i<10000000;i++)
		{
			sum++;
		}
		flagCaleEnd=1;
		printf("1");
		vTaskDelete(NULL);//自杀
	}
}

void Task2Function(void * param)
{
	while (1)
	{
		if(flagCaleEnd==1){
			printf("sum=%d\r\n",sum);
		}
	}
}

void TaskGenericFunction(void * param)
{
	while (1)
	{
		if(!flagUartUsed)
		{
			flagUartUsed=1;
			printf("%s\r\n",(char*)param);
			flagUartUsed=0;
            VTaskDelay(1);//延迟1ms,不然就一直执行任务4
		}
	}
}

/*-----------------------------------------------------------*/

int main( void )
{
	TaskHandle_t xHandleTask1;
		
#ifdef DEBUG
  debug();
#endif

	prvSetupHardware();

	printf("Hello, world!\r\n");

	xTaskCreate(Task1Function, "Task1", 100, NULL, 1, &xHandleTask1);
	//xTaskCreate(Task2Function, "Task2", 100, NULL, 1, NULL);
	
	xTaskCreate(TaskGenericFunction, "Task3", 100, "Task3 is running", 1, NULL);
	xTaskCreate(TaskGenericFunction, "Task4", 100, "Task4 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;
}

四.FreeRTOS的解决方案

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值