温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

unix 线程回调函数使用-pthread_cleanup_push && pthread_cleanup_pop

发布时间:2020-10-05 22:58:21 来源:网络 阅读:5772 作者:xieyihua 栏目:系统运维
  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; 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI