线程终止(二)





线程终止(二)

线程终止(二)

线程


线程清理函数

  
  
  1. #include <pthread.h>
  2. // 进栈
  3. void pthread_clreanup_push(void (*rtn)(void *), void *arg)
  4. // rtn : 进栈调用的函数
  5. // 出栈
  6. void pthread_clreanup_pop(int execute)
  7. // execute = 0, 清理函数将不被调用(出栈)

关于清理函数的应用, 我基本就直接用的是书上的代码了. 只是做了一点的修改而已.

  
  
  1. /*************************************************************************
  2. > File Name: cleanup_push_pop.cpp
  3. > Author: Function_Dou
  4. > Mail:
  5. > Created Time: 2018年02月07日 星期三 18时15分41秒
  6. ************************************************************************/
  7. #include <stdio.h>
  8. #include <pthread.h>
  9. #include "apue.h"
  10. void cleanup(void *arg)
  11. {
  12. printf("cleanup : %s\n", (char *)arg);
  13. }
  14. void *thr_fun1(void *arg)
  15. {
  16. char ch[] = "Fun1 first";
  17. char ch2[] = "Fun1 second";
  18. printf("create Fun1\n");
  19. // 将cleanup的函数入栈
  20. pthread_cleanup_push(cleanup, ch);
  21. pthread_cleanup_push(cleanup, ch2);
  22. printf("thread 1 push complete\n");
  23. // 沉睡 100s
  24. sleep(100);
  25. if(arg)
  26. return ((void *)1);
  27. // 将函数出栈
  28. pthread_cleanup_pop(0);
  29. pthread_cleanup_pop(0);
  30. return ((void *)1);
  31. }
  32. void *thr_fun2(void *arg)
  33. {
  34. char ch[] = "Fun2 first";
  35. char ch2[] = "Fun2 second";
  36. printf("create Fun2\n");
  37. // 将cleanup的函数入栈
  38. pthread_cleanup_push(cleanup, ch);
  39. pthread_cleanup_push(cleanup, ch2);
  40. printf("thread 2 push complete\n");
  41. if(arg)
  42. pthread_exit((void *)2);
  43. // 将函数出栈
  44. pthread_cleanup_pop(0);
  45. pthread_cleanup_pop(0);
  46. pthread_exit((void *)2);
  47. }
  48. int main(void)
  49. {
  50. int err;
  51. pthread_t pthreadid1, pthreadid2;
  52. void *tret;
  53. err = pthread_create(&pthreadid1, NULL, thr_fun1, (void *)1);
  54. if(err != 0)
  55. err_exit(err, "create 1 error");
  56. sleep(1);
  57. // 取消线程
  58. pthread_cancel(pthreadid1);
  59. // 获取返回值
  60. pthread_join(pthreadid1, &tret);
  61. printf("thread 1 exit code : %ld\n", (long)tret);
  62. err = pthread_create(&pthreadid2, NULL, thr_fun2, (void *)1);
  63. if(err != 0)
  64. err_exit(err, "creat 2 error");
  65. // 获取返回值
  66. pthread_join(pthreadid2, &tret);
  67. printf("thread 2 exit code : %ld\n", (long)tret);
  68. exit(0);
  69. }

在这个程序, 我主要是为了体现线程的取消.

运行结果

  
  
  1. // 注意 : 这里要用 g++. gcc可能回报错, 无法编译
  2. [root@localhost Pthread]# g++ cleanup_push_pop_线程清理函数.cpp -pthread
  3. [root@localhost Pthread]# ./a.out
  4. create Fun1
  5. thread 1 push complete
  6. cleanup : Fun1 second
  7. cleanup : Fun1 first
  8. thread 1 exit code : -1
  9. create Fun2
  10. thread 2 push complete
  11. cleanup : Fun2 second
  12. cleanup : Fun2 first
  13. thread 2 exit code : 2


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值