您好,登錄后才能下訂單哦!
- #if 0
- 線程結束時清理函數
- pthread_cleanup_push(),應該在線程開始的時候盡快執行初始化
- pthread_cleanup_pop(),當遇到以下三種各件會自動調用該函數,不需要PC執行到此函數
- 1.調用pthread_exit()
- 2.響應取消請求例如pthread_cancel()
- 3.當pthread_cleanup_pop(arg)參數arg為非0時會線程退出時自動執行
- 如果沒有以上三種情況,則不會執行;
- #endif
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <semaphore.h>
- sem_t sm0,sm1,sm2,sm3; //信號量定義 信號量為控制線程的執行
- void ret_fn(void*str)//線程退出調用的回調函數
- {
- 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);//將回調函數入棧
- fputs("thread0 is runing\n",stdout);
- pthread_cleanup_pop(0);//將回調函數出棧,當遇到特定的條件會自動調用,不需要PC指到此才執行
- 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("線程結束才會執行pthread_cleanup_pop\n",stdout);//線程結束才會執行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可以執行
- 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))//創建線程
- {
- 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(沒有調用回調函數),1(pthread_exit),2(pthread_cancel),3(參數非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繼續執行
- {
- 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;
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。