先给出代码吧:
-
#include "led.h"
-
#include "delay.h"
-
#include "sys.h"
-
#include "usart.h"
-
-
// FreeRTOS head file, add here.
-
#include "FreeRTOS.h"
-
#include "task.h"
-
#include "queue.h"
-
#include "list.h"
-
#include "portable.h"
-
#include "FreeRTOSConfig.h"
-
-
void task2(void *pvParameters);
-
-
xTaskHandle xTask2Handle;
-
-
static const char * Task1 = "task1 is running \r\n";
-
static const char * Task2 = "task2 is running \r\n";
-
-
-
void callFunction(void)
-
{
-
static int temp = 0;
-
-
temp ++;
-
printf("temp: %d\r\n", temp);
-
}
-
-
void task1(void * pvParameters)
-
{
-
char i = 0;
-
char *TaskName = (char *)pvParameters;
-
-
while(1)
-
{
-
printf(TaskName);
-
-
for(i =0; i<3; i++)
-
callFunction();
-
-
//xTaskCreate( task2, "Task2", configMINIMAL_STACK_SIZE, (void *)Task2, 2, &xTask2Handle);
-
vTaskDelay(1000/portTICK_RATE_MS);
-
}
-
}
-
-
int main(void)
-
{
-
// board initialize.
-
LED_Init();
-
uart_init(115200);
-
-
//create task here.
-
xTaskCreate( task1, "Task1", configMINIMAL_STACK_SIZE, (void *)Task1, 1, NULL);
-
-
// start scheduler now
-
vTaskStartScheduler();
-
-
return 0;
-
}
在task1
中三次调用了
callFunction()函数,那么
temp会怎么打印呢?
是不是没调用一次就创建一个变量temp呢?
运行结果是:
再看一个例子:如果有两个任务都在调用函数callFunction(),则temp会怎么打印呢?
给出代码:
-
#include "led.h"
-
#include "delay.h"
-
#include "sys.h"
-
#include "usart.h"
-
-
// FreeRTOS head file, add here.
-
#include "FreeRTOS.h"
-
#include "task.h"
-
#include "queue.h"
-
#include "list.h"
-
#include "portable.h"
-
#include "FreeRTOSConfig.h"
-
-
void task2(void *pvParameters);
-
-
static const char * Task1 = "task1 is running \r\n";
-
static const char * Task2 = "task2 is running \r\n";
-
-
-
void callFunction(void)
-
{
-
static int temp = 0;
-
-
temp ++;
-
printf("temp: %d\r\n", temp);
-
}
-
-
void task1(void * pvParameters)
-
{
-
char i = 0;
-
char *TaskName = (char *)pvParameters;
-
-
while(1)
-
{
-
printf(TaskName);
-
-
for(i =0; i<3; i++)
-
callFunction();
-
-
//xTaskCreate( task2, "Task2", configMINIMAL_STACK_SIZE, (void *)Task2, 2, &xTask2Handle);
-
vTaskDelay(1000/portTICK_RATE_MS);
-
}
-
}
-
-
-
-
-
-
void task2(void *pvParameters)
-
{
-
char i = 0;
-
char *TaskName = (char *)pvParameters;
-
-
while(1)
-
{
-
printf(TaskName);
-
-
for(i =0; i<3; i++)
-
callFunction();
-
-
vTaskDelay(1000/portTICK_RATE_MS);
-
}
-
}
-
-
-
int main(void)
-
{
-
// board initialize.
-
LED_Init();
-
uart_init(115200);
-
-
//create task here.
-
xTaskCreate( task1, "Task1", configMINIMAL_STACK_SIZE, (void *)Task1, 1, NULL);
-
xTaskCreate( task2, "Task2", configMINIMAL_STACK_SIZE, (void *)Task2, 1, NULL);
-
-
// start scheduler now
-
vTaskStartScheduler();
-
-
return 0;
-
}
打印结果又是啥样呢?
看到这个结果,是不是就明白什么了吗?
如果用static定义了局部静态变量,则调用的函数,都是在共享这个变量。
如果没有用这个static修饰的话,则有啥区别呢?
阅读(891) | 评论(0) | 转发(0) |