- #if 0
- 線程結束時清理函數(shù)
- pthread_cleanup_push(),應該在線程開始的時候盡快執(zhí)行初始化
- pthread_cleanup_pop(),當遇到以下三種各件會自動調用該函數(shù),不需要PC執(zhí)行到此函數(shù)
- 1.調用pthread_exit()
- 2.響應取消請求例如pthread_cancel()
- 3.當pthread_cleanup_pop(arg)參數(shù)arg為非0時會線程退出時自動執(zhí)行
- 如果沒有以上三種情況,則不會執(zhí)行;
- #endif
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <semaphore.h>
- sem_t sm0,sm1,sm2,sm3; //信號量定義 信號量為控制線程的執(zhí)行
- void ret_fn(void*str)//線程退出調用的回調函數(shù)
- {
- fputs(str,stdout);
- }
- void thread0(void)
- {
- if(0 != sem_wait(&sm0))//信號量進行阻塞
- {
- perror("sem_wait_0");
- }
- char *str = "thead0_return_FN is runing\n";
- pthread_cleanup_push(ret_fn,str);//將回調函數(shù)入棧
- fputs("thread0 is runing\n",stdout);
- pthread_cleanup_pop(0);//將回調函數(shù)出棧,當遇到特定的條件會自動調用,不需要PC指到此才執(zhí)行
- return;
- }
- void thread1(void)
- {
- if(0 != sem_wait(&sm1))
- {
- perror("sem_wait_1");
- }
- char *str = "thead1_return_FN is runing\n";
- pthread_cleanup_push(ret_fn,str);
- fputs("thread1 is runing\n",stdout);
- pthread_exit((void*)1);
- pthread_cleanup_pop(0);
- }
- void thread2(void)
- {
- if(0 != sem_wait(&sm2))
- {
- perror("sem_wait_2");
- }
- char *str = "thead2_return_FN is runing\n";
- pthread_cleanup_push(ret_fn,str);
- fputs("thread2 is runing\n",stdout);
- pthread_cleanup_pop(1);
- fputs("線程結束才會執(zhí)行pthread_cleanup_pop\n",stdout);//線程結束才會執(zhí)行pthread_cleanup_pop
- }
- void thread3(void)
- {
- char *str = "thead3_return_FN is runing\n";
- pthread_cleanup_push(ret_fn,str);
- if(0 != sem_wait(&sm3))
- {
- perror("sem_wait_3");
- }
- fputs("thread3 is runing\n",stdout);
- usleep(100);// 讓主線程中的pthread_cancel可以執(zhí)行
- pthread_cleanup_pop(0);
- }
- int main(void)
- {
- pthread_t thd0,thd1,thd2,thd3;
- if(0 != sem_init(&sm0,0,0))//初始化信號量
- {
- perror("sem_init_0");
- exit(1);
- }
- if(0 != sem_init(&sm1,0,0))
- {
- perror("sem_init_0");
- exit(1);
- }
- if(0 != sem_init(&sm2,0,0))
- {
- perror("sem_init_0");
- exit(1);
- }
- if(0 != sem_init(&sm3,0,0))
- {
- perror("sem_init_0");
- exit(1);
- }
- if(0 != pthread_create(&thd0,NULL,(void*)thread0,NULL))//創(chuàng)建線程
- {
- perror("pthread_create_0\n");
- exit(1);
- }
- if(0 != pthread_create(&thd1,NULL,(void*)thread1,NULL))
- {
- perror("pthread_create_1\n");
- exit(1);
- }
- if(0 != pthread_create(&thd2,NULL,(void*)thread2,NULL))
- {
- perror("pthread_create_2\n");
- exit(1);
- }
- if(0 != pthread_create(&thd3,NULL,(void*)thread3,NULL))
- {
- perror("pthread_create_3\n");
- exit(1);
- }
- unsigned char status = 0xF;//控制重復測試掩碼
- while(1)
- {
- fputs("請輸入測試選項:0(沒有調用回調函數(shù)),1(pthread_exit),2(pthread_cancel),3(參數(shù)非0 pthread_cleanup_pop)\n",stdout);
- int in;
- scanf("%d",&in);
- switch(in)
- {
- case 0:
- {
- if(!(status & 0x1))
- {
- fputs("這個項目已測試過,請選擇其它,謝謝\n",stdout);
- break;
- }
- if(0 != sem_post(&sm0))//激活信號量,讓子線程THD0繼續(xù)執(zhí)行
- {
- perror("sem_post_0\n");
- }
- status &= 0xe;
- break;
- }
- case 1:
- {
- if(!(status & 0x2))
- {
- fputs("這個項目已測試過,請選擇其它,謝謝\n",stdout);
- break;
- }
- if(0 != sem_post(&sm1))
- {
- perror("sem_post_1\n");
- }
- status &= 0xc;
- break;
- }
- case 2:
- {
- if(!(status & 0x4))
- {
- fputs("這個項目已測試過,請選擇其它,謝謝\n",stdout);
- break;
- }
- if(0 != sem_post(&sm2))
- {
- perror("sem_post_2\n");
- }
- status &= 0xb;
- break;
- }
- case 3:
- {
- if(!(status & 0x8))
- {
- fputs("這個項目已測試過,請選擇其它,謝謝\n",stdout);
- break;
- }
- if(0 != sem_post(&sm3))
- {
- perror("sem_post_3\n");
- }
- pthread_cancel(thd3);
- status &= 0x7;
- break;
- }
- default: break;
- }
- sleep(1);
- }
- 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)