Linux多线程实例练习 - pthread_cancel

1、代码 xx_pthread_cancel.c

  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4.  
  5. #define debug_Msg(fmt, arg...)\
  6. do{\
  7. printf("%s %d : ", __FILE__, __LINE__);\
  8. printf(fmt, ##arg);\
  9. }while()
  10.  
  11. #define ENABLE_X
  12. char * pe = "enable return";
  13. void * state_Enable(void *arg)
  14. {
  15. int i = ;
  16. int iExit = ;
  17. while(i < && iExit == )
  18. {
  19. debug_Msg("state Enable : [%d]\n", i);
  20. i++;
  21. sleep();
  22. }
  23. char * p = pe;
  24. return p;
  25. }
  26.  
  27. #define DISABLE_X
  28. char * pd = "disable return";
  29. void * state_Disable(void * arg)
  30. {
  31. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  32. int i = ;
  33. int iExit = ;
  34. while(i < && iExit == )
  35. {
  36. debug_Msg("state Disable : [%d]\n", i);
  37. i++;
  38. sleep();
  39. }
  40. char * p = pd;
  41. return p;
  42. }
  43.  
  44. int main()
  45. {
  46. #ifdef ENABLE_X
  47. pthread_t pid;
  48. pthread_create(&pid, NULL, state_Enable, NULL);
  49. sleep();
  50. pthread_cancel(pid);
  51. void * p = NULL;
  52. printf("init with : [%08X]\n", (unsigned int)p);
  53. pthread_join(pid, &p);
  54. printf("pe addr : [%08X]\n", (unsigned int)pe);
  55. printf("over with : [%08X]\n", (unsigned int)p);
  56. #endif
  57.  
  58. #ifdef DISABLE_X
  59. pthread_t pDis;
  60. pthread_create(&pDis, NULL, state_Disable, NULL);
  61. sleep();
  62. pthread_cancel(pDis);
  63. p = NULL;
  64. printf("init with : [%08X]\n", (unsigned int)p);
  65. pthread_join(pDis, &p);
  66. printf("pd addr : [%08X]\n", (unsigned int)pd);
  67. printf("over with : [%08X]\n", (unsigned int)p);
  68. #endif
  69. }

2、CentOS 编译通过

  1. g++ -g -c -o xx_pthread_cancel.o xx_pthread_cancel.c
  2. g++ -g -o xx_pthread_cancel xx_pthread_cancel.o -lpthread

3、运行结果

  1. $ ./xx_pthread_cancel
  2. xx_pthread_cancel.c : state Enable : []
  3. xx_pthread_cancel.c : state Enable : []
  4. xx_pthread_cancel.c : state Enable : []
  5. init with : []
  6. pe addr : []
  7. over with : [FFFFFFFF]
  8. xx_pthread_cancel.c : state Disable : []
  9. xx_pthread_cancel.c : state Disable : []
  10. xx_pthread_cancel.c : state Disable : []
  11. init with : []
  12. xx_pthread_cancel.c : state Disable : []
  13. xx_pthread_cancel.c : state Disable : []
  14. xx_pthread_cancel.c : state Disable : []
  15. xx_pthread_cancel.c : state Disable : []
  16. xx_pthread_cancel.c : state Disable : []
  17. xx_pthread_cancel.c : state Disable : []
  18. xx_pthread_cancel.c : state Disable : []
  19. pd addr : []
  20. over with : []

Linux多线程实例练习 - pthread_cancel()的更多相关文章

  1. Linux多线程实例练习 - pthread_exit() 与 pthread_join()

    Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...

  2. Linux多线程实例练习 - pthread_create()

    Linux多线程实例练习 pthread_create():创建一个线程 int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, ...

  3. Linux多线程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  4. Linux多线程实例 定时重启httpd和mysqld

    #include <stdio.h> #include <pthread.h> void *start_routine(void *arg) { while(1) { syst ...

  5. Linux多线程编程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  6. 笔记整理--Linux多线程

    Unix高级环境编程系列笔记 (2013/11/17 14:26:38) Unix高级环境编程系列笔记 出处信息 通过这篇文字,您将能够解答如下问题: 如何来标识一个线程? 如何创建一个新线程? 如何 ...

  7. 【操作系统作业-lab4】 linux 多线程编程和调度器

    linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...

  8. linux多线程同步pthread_cond_XXX条件变量的理解

    在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-- ...

  9. Linux 多线程应用中如何编写安全的信号处理函数

    http://blog.163.com/he_junwei/blog/static/1979376462014021105242552/ http://www.ibm.com/developerwor ...

随机推荐

  1. [Linux] 取得服务器版本

    1) 登录到服务器执行 lsb_release -a ,即可列出所有版本信息,例如: [root@3.5.5Biz-46 ~]# lsb_release -a LSB Version: 1.3 Dis ...

  2. W-数据库基础

    数据库系统由三部分组成:数据库(DB).数据库管理系统(DBMS)和数据库应用系统 数据加是用来存储数据的,里面存储两大类数据:用户数据及系统数据/数据字典,具体为系统中的用户以及用户孤权限,各种统计 ...

  3. 深入理解ASP.NET 5的依赖注入

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:ASP.NET 5整个底层都架构于依赖注入机制之下,今天介绍的文章详细介绍了内置依赖注 ...

  4. 第二十五篇:在SOUI中做事件分发处理

    不同的SOUI控件可以产生不同的事件.SOUI系统中提供了两种事件处理方式:事件订阅 + 事件处理映射表(参见第八篇:SOUI中控件事件的响应) 事件订阅由于直接将事件及事件处理函数连接,不存在事件分 ...

  5. 数据分析(4):Scipy

    科学计算 最小二乘leastsq # -*- coding: utf-8 -*- def func(x,p): # p 参数列表 A,k,theta = p; # 可以一一对应赋值 return A* ...

  6. 遍历注册表回调函数(仿PCHunter CmpBack)

    遍历注册表回调函数(仿PCHunter CmpBack) typedef struct _CAPTURE_REGISTRY_MANAGER { PDEVICE_OBJECT deviceObject; ...

  7. 视觉差双排listview效果

    https://github.com/bavariama1/ListBuddies

  8. Eclipse首字母快捷设置

    windows->Preference  ,然后:

  9. JavaScript设计模式——状态模式

    状态和行为: 所谓对象的状态,通常指的就是对象实例的属性的值:而行为指的就是对象的功能,再具体点说,行为大多可以对应到方法上. 状态模式的功能就是分离状态的行为,通过维护状态的变化,来调用不同状态对应 ...

  10. address元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...