Chinaunix首页 | 论坛 | 博客
  • 博客访问: 211708
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 978
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-10 15:46
个人简介

青春无悔

文章分类

全部博文(93)

文章存档

2015年(16)

2014年(77)

我的朋友

分类: C/C++

2015-04-01 20:32:12

如果一个函数可以安全地被多个任务调用,或是在任务与中断中均可调用,则这个
函数是可重入的。 
每个任务都单独维护自己的栈空间及其自身在的内存寄存器组中的值。如果一个函
数除了访问自己栈空间上分配的数据或是内核寄存器中的数据外,不会访问其它任何
数据,则这个函数就是可重入的

点击(此处)折叠或打开

    1. 可重入函数示例 
  1. /* A parameter is passed into the function. This will either be
  2. passed on the stack or in a CPU register. Either way is safe as
  3. each task maintains its own stack and its own set of register
  4. values. */
  5. long lAddOneHundered( long lVar1 )
  6. {
  7. /* This function scope variable will also be allocated to the stack
  8. or a register, depending on compiler and optimization level. Each
  9. task or interrupt that calls this function will have its own copy
  10. of lVar2. */
  11. long lVar2;
  12. lVar2 = lVar1 + 100;
  13.  
  14. /* Most likely the return value will be placed in a CPU register,
  15. although it too could be placed on the stack. */
  16. return lVar2;
  17. } 

  18. 2.不可重入函数示例 
  19. /* In this case lVar1 is a global variable so every task that calls
  20. the function will be accessing the same single copy of the variable. */
  21. long lVar1;
  22. long lNonsenseFunction( void )
  23. {
  24. /* This variable is static so is not allocated on the stack. Each task
  25. that calls the function will be accessing the same single copy of the
  26. variable. */
  27. static long lState = 0;
  28. long lReturn;
  29.  
  30. switch( lState )
  31. {
  32. case 0 : lReturn = lVar1 + 10;
  33. lState = 1;
  34. break;
  35.  
  36. case 1 : lReturn = lVar1 + 20;
  37. lState = 0;
  38. break;
  39. }
  40. }


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