189 8069 5689

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

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

 

成都创新互联-专业网站定制、快速模板网站建设、高性价比茂南网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式茂南网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖茂南地区。费用合理售后完善,十年实体公司更值得信赖。


文章名称:unix线程回调函数使用-pthread_cleanup_push&&pthread_cleanup_pop
网站路径:http://ptruijie.cn/article/jgjepg.html

其他资讯