Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1236242
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: C/C++

2017-08-24 15:57:23

标题两个功能是由gcc的Attributes constructor和destructor实现的。

constructor属性 :使函数在main()函数之前执行
destructor属性   :使函数在main()函数完成或调用exit()之后被执行


这两个属性都还可以指定优先级,控制使用修饰的函数的执行顺序,优先级的值必须大于100,因为0到100之间的优先级由gcc来使用,优先级的值越小,优先级越高,会优先执行。

注意:如果是接收到信号退出,例如SIGSEGV或者SIGKILL信号,destructor属性修饰的函数则不会被调用。

参考:http://blog.csdn.net/justlinux2010/article/details/11695645 写的源码,发现没怎么说明优先级,那优先级是如何使用的呢。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. static void __attribute__((constructor)) pre_main1(void)
  4. {
  5. printf("Come to %s\n", __func__);
  6. }

  7. static void __attribute__((constructor)) pre_main2(void)
  8. {
  9. printf("Come to %s\n", __func__);
  10. }

  11. static void __attribute__((constructor)) pre_main3(void)
  12. {
  13. printf("Come to %s\n", __func__);
  14. }

  15. int main(void)
  16. {
  17. printf("Exiting.....\n");
  18. return 0;
  19. }

  20. static void __attribute__((destructor)) back_main1(void)
  21. {
  22. printf("Come to %s\n", __func__);
  23. }

  24. static void __attribute__((destructor)) back_main2(void)
  25. {
  26. printf("Come to %s\n", __func__);
  27. }

执行结果:
Come to pre_main1
Come to pre_main2
Come to pre_main3
Exiting.....
Come to back_main2
Come to back_main1
看一下gcc在线文档
Declaring Attributes of Functions对这两个属性的说明: constructor destructor constructor (priority) destructor (priority)

The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.

You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see ). However, at present, the order in which constructors for C++ objects with static storage duration and functions decorated with attribute constructor are invoked is unspecified. In mixed declarations, attribute init_priority can be used to impose a specific ordering.

英文部分和一开始介绍的基本相同,补充了一点是 constructor 属性函数优先级从小到大,destructor 是由大到小 顺序执行。

对例子中的代码加优先级:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. static void __attribute__((constructor)) pre_main1(void)
  4. {
  5. printf("Come to %s\n", __func__);
  6. }

  7. static void __attribute__((constructor(101))) pre_main2(void)
  8. {
  9. printf("Come to %s\n", __func__);
  10. }

  11. static void __attribute__((constructor(102))) pre_main3(void)
  12. {
  13. printf("Come to %s\n", __func__);
  14. }

  15. int main(void)
  16. {
  17. printf("Exiting.....\n");
  18. return 0;
  19. }

  20. static void __attribute__((destructor(101))) back_main1(void)
  21. {
  22. printf("Come to %s\n", __func__);
  23. }

  24. static void __attribute__((destructor)) back_main2(void)
  25. {
  26. printf("Come to %s\n", __func__);
  27. }
执行结果:
Come to pre_main2
Come to pre_main3
Come to pre_main1
Exiting.....
Come to back_main2
Come to back_main1

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