Chinaunix首页 | 论坛 | 博客
  • 博客访问: 831137
  • 博文数量: 281
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2770
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 19:45
个人简介

邮箱:zhuimengcanyang@163.com 痴爱嵌入式技术的蜗牛

文章分类
文章存档

2020年(1)

2018年(1)

2017年(56)

2016年(72)

2015年(151)

分类: 嵌入式

2015-09-21 09:28:40

先给出代码吧:

点击(此处)折叠或打开

  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "usart.h"

  5. // FreeRTOS head file, add here.
  6. #include "FreeRTOS.h"
  7. #include "task.h"
  8. #include "queue.h"
  9. #include "list.h"
  10. #include "portable.h"
  11. #include "FreeRTOSConfig.h"

  12. void task2(void *pvParameters);

  13. xTaskHandle xTask2Handle;

  14. static const char * Task1 = "task1 is running \r\n";
  15. static const char * Task2 = "task2 is running \r\n";


  16. void callFunction(void)
  17. {
  18.     static int temp = 0;
  19.     
  20.     temp ++;
  21.     printf("temp: %d\r\n", temp);
  22. }

  23. void task1(void * pvParameters)
  24. {
  25.     char i = 0;
  26.     char *TaskName = (char *)pvParameters;

  27.     while(1)
  28.     {
  29.         printf(TaskName);
  30.         
  31.         for(i =0; i<3; i++)
  32.           callFunction();
  33.         
  34.         //xTaskCreate( task2, "Task2", configMINIMAL_STACK_SIZE, (void *)Task2, 2, &xTask2Handle);
  35.         vTaskDelay(1000/portTICK_RATE_MS);
  36.     }
  37. }

  38. int main(void)
  39. {
  40.     // board initialize.
  41.     LED_Init();             
  42.     uart_init(115200);
  43.     
  44.     //create task here.
  45.     xTaskCreate( task1, "Task1", configMINIMAL_STACK_SIZE, (void *)Task1, 1, NULL);
  46.     
  47.     // start scheduler now
  48.     vTaskStartScheduler();

  49.     return 0;
  50. }
在task1中三次调用了callFunction()函数,那么temp会怎么打印呢?
是不是没调用一次就创建一个变量temp呢?

运行结果是:


再看一个例子:如果有两个任务都在调用函数callFunction(),则temp会怎么打印呢?
给出代码:

点击(此处)折叠或打开

  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "usart.h"

  5. // FreeRTOS head file, add here.
  6. #include "FreeRTOS.h"
  7. #include "task.h"
  8. #include "queue.h"
  9. #include "list.h"
  10. #include "portable.h"
  11. #include "FreeRTOSConfig.h"

  12. void task2(void *pvParameters);

  13. static const char * Task1 = "task1 is running \r\n";
  14. static const char * Task2 = "task2 is running \r\n";


  15. void callFunction(void)
  16. {
  17.     static int temp = 0;
  18.     
  19.     temp ++;
  20.     printf("temp: %d\r\n", temp);
  21. }

  22. void task1(void * pvParameters)
  23. {
  24.     char i = 0;
  25.     char *TaskName = (char *)pvParameters;

  26.     while(1)
  27.     {
  28.         printf(TaskName);
  29.         
  30.         for(i =0; i<3; i++)
  31.             callFunction();
  32.         
  33.         //xTaskCreate( task2, "Task2", configMINIMAL_STACK_SIZE, (void *)Task2, 2, &xTask2Handle);
  34.         vTaskDelay(1000/portTICK_RATE_MS);
  35.     }
  36. }





  37. void task2(void *pvParameters)
  38. {
  39.     char i = 0;
  40.     char *TaskName = (char *)pvParameters;

  41.     while(1)
  42.     {
  43.         printf(TaskName);
  44.         
  45.         for(i =0; i<3; i++)
  46.             callFunction();
  47.         
  48.         vTaskDelay(1000/portTICK_RATE_MS);
  49.     }
  50. }


  51. int main(void)
  52. {
  53.     // board initialize.
  54.     LED_Init();             
  55.     uart_init(115200);
  56.     
  57.     //create task here.
  58.     xTaskCreate( task1, "Task1", configMINIMAL_STACK_SIZE, (void *)Task1, 1, NULL);
  59.     xTaskCreate( task2, "Task2", configMINIMAL_STACK_SIZE, (void *)Task2, 1, NULL);
  60.    
  61.     // start scheduler now
  62.     vTaskStartScheduler();

  63.     return 0;
  64. }

打印结果又是啥样呢?


看到这个结果,是不是就明白什么了吗?

如果用static定义了局部静态变量,则调用的函数,都是在共享这个变量。
如果没有用这个static修饰的话,则有啥区别呢?


阅读(897) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~