一.简介
二.同步的例子:有缺陷
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的解决方案