unix 线程回调函数使用-pthread_cleanup_push && pthread_cleanup_pop
admin
2023-02-04 19:40:04
0
  1. #if 0 
  2. 线程结束时清理函数 
  3. pthread_cleanup_push(),应该在线程开始的时候尽快执行初始化 
  4. pthread_cleanup_pop(),当遇到以下三种各件会自动调用该函数,不需要PC执行到此函数 
  5. 1.调用pthread_exit() 
  6. 2.响应取消请求例如pthread_cancel() 
  7. 3.当pthread_cleanup_pop(arg)参数arg为非0时会线程退出时自动执行 
  8.  
  9. 如果没有以上三种情况,则不会执行; 
  10. #endif 
  11.  
  12. #include <pthread.h> 
  13. #include <stdio.h> 
  14. #include <stdlib.h> 
  15. #include <unistd.h> 
  16. #include <semaphore.h> 
  17.  
  18. sem_t sm0,sm1,sm2,sm3; //信号量定义 信号量为控制线程的执行 
  19.  
  20. void ret_fn(void*str)//线程退出调用的回调函数 
  21.     fputs(str,stdout); 
  22.  
  23.  
  24. void thread0(void) 
  25.     if(0 != sem_wait(&sm0))//信号量进行阻塞 
  26.     { 
  27.         perror("sem_wait_0"); 
  28.     } 
  29.      
  30.     char *str = "thead0_return_FN is runing\n"
  31.     pthread_cleanup_push(ret_fn,str);//将回调函数入栈 
  32.     fputs("thread0 is runing\n",stdout); 
  33.     pthread_cleanup_pop(0);//将回调函数出栈,当遇到特定的条件会自动调用,不需要PC指到此才执行 
  34.     return; 
  35. void thread1(void) 
  36.     if(0 != sem_wait(&sm1)) 
  37.     { 
  38.         perror("sem_wait_1"); 
  39.     } 
  40.     char *str = "thead1_return_FN is runing\n"
  41.     pthread_cleanup_push(ret_fn,str); 
  42.     fputs("thread1 is runing\n",stdout); 
  43.     pthread_exit((void*)1); 
  44.     pthread_cleanup_pop(0); 
  45.  
  46. void thread2(void) 
  47.     if(0 != sem_wait(&sm2)) 
  48.     { 
  49.         perror("sem_wait_2"); 
  50.     } 
  51.     char *str = "thead2_return_FN is runing\n"
  52.     pthread_cleanup_push(ret_fn,str); 
  53.     fputs("thread2 is runing\n",stdout); 
  54.     pthread_cleanup_pop(1); 
  55.     fputs("线程结束才会执行pthread_cleanup_pop\n",stdout);//线程结束才会执行pthread_cleanup_pop 
  56. void thread3(void) 
  57.     char *str = "thead3_return_FN is runing\n"
  58.     pthread_cleanup_push(ret_fn,str); 
  59.     if(0 != sem_wait(&sm3)) 
  60.     { 
  61.         perror("sem_wait_3"); 
  62.     } 
  63.     fputs("thread3 is runing\n",stdout); 
  64.     usleep(100);// 让主线程中的pthread_cancel可以执行 
  65.     pthread_cleanup_pop(0); 
  66.  
  67.  
  68. int main(void) 
  69.  
  70.     pthread_t thd0,thd1,thd2,thd3; 
  71.      
  72.     if(0 != sem_init(&sm0,0,0))//初始化信号量 
  73.     { 
  74.         perror("sem_init_0"); 
  75.         exit(1); 
  76.     } 
  77.     if(0 != sem_init(&sm1,0,0)) 
  78.     { 
  79.         perror("sem_init_0"); 
  80.         exit(1); 
  81.     } 
  82.     if(0 != sem_init(&sm2,0,0)) 
  83.     { 
  84.         perror("sem_init_0"); 
  85.         exit(1); 
  86.     } 
  87.     if(0 != sem_init(&sm3,0,0)) 
  88.     { 
  89.         perror("sem_init_0"); 
  90.         exit(1); 
  91.     } 
  92.      
  93.     if(0 != pthread_create(&thd0,NULL,(void*)thread0,NULL))//创建线程 
  94.     { 
  95.         perror("pthread_create_0\n"); 
  96.         exit(1); 
  97.     } 
  98.     if(0 != pthread_create(&thd1,NULL,(void*)thread1,NULL)) 
  99.     { 
  100.         perror("pthread_create_1\n"); 
  101.         exit(1); 
  102.     } 
  103.     if(0 != pthread_create(&thd2,NULL,(void*)thread2,NULL)) 
  104.     { 
  105.         perror("pthread_create_2\n"); 
  106.         exit(1); 
  107.     } 
  108.     if(0 != pthread_create(&thd3,NULL,(void*)thread3,NULL)) 
  109.     { 
  110.         perror("pthread_create_3\n"); 
  111.         exit(1); 
  112.     } 
  113.     unsigned char status = 0xF;//控制重复测试掩码 
  114.      
  115.     while(1) 
  116.     { 
  117.         fputs("请输入测试选项:0(没有调用回调函数),1(pthread_exit),2(pthread_cancel),3(参数非0 pthread_cleanup_pop)\n",stdout); 
  118.         int in; 
  119.         scanf("%d",&in); 
  120.         switch(in) 
  121.         { 
  122.             case 0: 
  123.                 { 
  124.                     if(!(status & 0x1)) 
  125.                     { 
  126.                         fputs("这个项目已测试过,请选择其它,谢谢\n",stdout); 
  127.                         break; 
  128.                     } 
  129.                     if(0 != sem_post(&sm0))//激活信号量,让子线程THD0继续执行 
  130.                     { 
  131.                         perror("sem_post_0\n"); 
  132.                     } 
  133.                     status &= 0xe; 
  134.                     break; 
  135.                 } 
  136.             case 1: 
  137.                 { 
  138.                     if(!(status & 0x2)) 
  139.                     { 
  140.                         fputs("这个项目已测试过,请选择其它,谢谢\n",stdout); 
  141.                         break; 
  142.                     } 
  143.                     if(0 != sem_post(&sm1)) 
  144.                     { 
  145.                         perror("sem_post_1\n"); 
  146.                     } 
  147.                     status &= 0xc; 
  148.                     break; 
  149.                 }        
  150.             case 2: 
  151.                 { 
  152.                     if(!(status & 0x4)) 
  153.                     { 
  154.                         fputs("这个项目已测试过,请选择其它,谢谢\n",stdout); 
  155.                         break; 
  156.                     } 
  157.                     if(0 != sem_post(&sm2)) 
  158.                     { 
  159.                         perror("sem_post_2\n"); 
  160.                     } 
  161.                     status &= 0xb; 
  162.                     break; 
  163.                 } 
  164.             case 3: 
  165.                 { 
  166.                     if(!(status & 0x8)) 
  167.                     { 
  168.                         fputs("这个项目已测试过,请选择其它,谢谢\n",stdout); 
  169.                         break; 
  170.                     } 
  171.                     if(0 != sem_post(&sm3)) 
  172.                     { 
  173.                         perror("sem_post_3\n"); 
  174.                     } 
  175.                     pthread_cancel(thd3); 
  176.                     status &= 0x7; 
  177.                     break; 
  178.                 } 
  179.             default: break; 
  180.         }    
  181.         sleep(1); 
  182.     } 
  183.  
  184.      
  185.     return 0; 

 

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...