欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

unix線程回調函數(shù)使用-pthread_cleanup_push&&pthread_cleanup_pop

  1. #if 0 
  2. 線程結束時清理函數(shù) 
  3. pthread_cleanup_push(),應該在線程開始的時候盡快執(zhí)行初始化 
  4. pthread_cleanup_pop(),當遇到以下三種各件會自動調用該函數(shù),不需要PC執(zhí)行到此函數(shù) 
  5. 1.調用pthread_exit() 
  6. 2.響應取消請求例如pthread_cancel() 
  7. 3.當pthread_cleanup_pop(arg)參數(shù)arg為非0時會線程退出時自動執(zhí)行 
  8.  
  9. 如果沒有以上三種情況,則不會執(zhí)行; 
  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; //信號量定義 信號量為控制線程的執(zhí)行 
  19.  
  20. void ret_fn(void*str)//線程退出調用的回調函數(shù) 
  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);//將回調函數(shù)入棧 
  32.     fputs("thread0 is runing\n",stdout); 
  33.     pthread_cleanup_pop(0);//將回調函數(shù)出棧,當遇到特定的條件會自動調用,不需要PC指到此才執(zhí)行 
  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("線程結束才會執(zhí)行pthread_cleanup_pop\n",stdout);//線程結束才會執(zhí)行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可以執(zhí)行 
  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))//創(chuàng)建線程 
  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(沒有調用回調函數(shù)),1(pthread_exit),2(pthread_cancel),3(參數(shù)非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繼續(xù)執(zhí)行 
  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; 

 

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比茂南網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式茂南網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋茂南地區(qū)。費用合理售后完善,十年實體公司更值得信賴。

標題名稱:unix線程回調函數(shù)使用-pthread_cleanup_push&&pthread_cleanup_pop
本文路徑:http://chinadenli.net/article36/jgjepg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)微信小程序App開發(fā)移動網(wǎng)站建設標簽優(yōu)化做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設計公司