關鍵:
pthread_cancel函數發送終止信號
pthread_setcancelstate函數設置終止方式
pthread_testcancel函數取消線程(另一功能是:設置取消點)
1 線程取消的定義
一般情況下,線程在其主體函數退出的時候會自動終止,但同時也可以因為接收到另一個線程發來的終止(取消)請求而強制終止。
2 線程取消的語義
線程取消的方法是向目標線程發Cancel信號(pthread_cancel函數發送Cancel信號),但如何處理Cancel信號則由目標線程自己決定,或者忽略、或者立即終止、或者繼續運行至Cancelation-point(取消點),由不同的Cancelation狀態(pthread_setcancelstate函數設置狀態)決定。
線程接收到CANCEL信號的缺省處理(即pthread_create()創建線程的缺省狀態)是繼續運行至取消點,也就是說設置一個CANCELED狀態,線程繼續運行,只有運行至Cancelation-point的時候才會退出。
3 取消點
根據POSIX標準,pthread_join()、pthread_testcancel()、pthread_cond_wait()、 pthread_cond_timedwait()、sem_wait()、sigwait()等函數以及read()、write()等會引起阻塞的系統調用都是Cancelation-point,而其他pthread函數都不會引起Cancelation動作。但是pthread_cancel的手冊頁聲稱,由于LinuxThread庫與C庫結合得不好,因而目前C庫函數都不是Cancelation-point;但CANCEL信號會使線程從阻塞的系統調用中退出,并置EINTR錯誤碼,因此可以在需要作為Cancelation-point的系統調用前后調用 pthread_testcancel(),從而達到POSIX標準所要求的目標,即如下代碼段:
pthread_testcancel();
retcode = read(fd, buffer, length);
pthread_testcancel();
4 程序設計方面的考慮
如果線程處于無限循環中,且循環體內沒有執行至取消點的必然路徑,則線程無法由外部其他線程的取消請求而終止。因此在這樣的循環體的必經路徑上應該加入pthread_testcancel()調用。
5 與線程取消相關的pthread函數
int pthread_cancel(pthread_t thread)
發送終止信號給thread線程,如果成功則返回0,否則為非0值。發送成功并不意味著thread會終止。
int pthread_setcancelstate(int state, int *oldstate)
設置本線程對Cancel信號的反應,state有兩種值:PTHREAD_CANCEL_ENABLE(缺省)和 PTHREAD_CANCEL_DISABLE,分別表示收到信號后設為CANCLED狀態和忽略CANCEL信號繼續運行;old_state如果不為 NULL則存入原來的Cancel狀態以便恢復。
int pthread_setcanceltype(int type, int *oldtype)
設置本線程取消動作的執行時機,type由兩種取值:PTHREAD_CANCEL_DEFFERED和 PTHREAD_CANCEL_ASYCHRONOUS,僅當Cancel狀態為Enable時有效,分別表示收到信號后繼續運行至下一個取消點再退出和立即執行取消動作(退出);oldtype如果不為NULL則存入運來的取消動作類型值。
void pthread_testcancel(void)
功能一:設置取消點;
功能二:檢查本線程是否處于Canceld狀態,如果是,則進行取消動作,否則直接返回。
代碼:
#include <stdio.h>#include <errno.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>#define THREAD_MAX 4pthread_mutex_t mutex;pthread_t thread[THREAD_MAX];static int tries;static int started;void print_it(int *arg){pthread_t tid;tid = pthread_self();printf("Thread %lx was canceled on its %d try./n",tid,*arg);}void *Search_Num(int arg){pthread_t tid;int num;int k=0,h=0,j;int ntries;tid = pthread_self();/*while(pthread_mutex_trylock(&mutex) == EBUSY){printf("**************busy****************/n");pthread_testcancel();}*/srand(arg);num = rand()&0xFFFFFF;//pthread_mutex_unlock(&mutex);printf("thread num %lx/n",tid);ntries = 0;pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);pthread_cleanup_push((void *)print_it,(void *)&ntries);while(1){num = (num+1)&0xffffff;ntries++;if(arg == num){//只允許一個線程操作此處while(pthread_mutex_trylock(&mutex) == EBUSY) { //一個線程操作后其余線程進入次循環掛起,等待pthread_cancel函數發送cancel信號終止線程k++;if(k == 10000){printf("----------2busy2-----------/n");}pthread_testcancel();}tries = ntries;//pthread_mutex_unlock(&mutex); //如果加上這句話,將會有好幾個線程找到主函數中設定的值pidprintf("Thread %lx found the number!/n",tid);for(j = 0;j<THREAD_MAX;j++){if(thread[j]!=tid){pthread_cancel(thread[j]);}}break;}if(ntries%100 == 0){h++;/*線程阻塞,其他線程爭奪資源,或者是等待pthread_cancel函數發送cancel信號終止線程*/pthread_testcancel();/*這是為了弄明白pthread_testcancel函數的作用而設置的代碼段*/if(h == 10000){h = 0;printf("----------thread num %lx-------------/n",tid);}}}pthread_cleanup_pop(0);return (void *)0;}int main(){int i,pid;pid = getpid(); //設置要查找的數pthread_mutex_init(&mutex,NULL);printf("Search the num of %d/n",pid);for(started = 0; started < THREAD_MAX; started++){pthread_create(&thread[started],NULL,(void *)Search_Num,(void *)pid);}for(i = 0; i < THREAD_MAX; i++){printf("-----------i = %d--------------/n",i);pthread_join(thread[i],NULL);}printf("It took %d tries ot find the number!/n",tries);return 0;}
運行結果:
Search the num of 6531-----------i = 0--------------thread num b6fbcb70thread num b67bbb70thread num b5fbab70thread num b77bdb70----------thread num b67bbb70-------------Thread b67bbb70 found the number!----------thread num b6fbcb70-----------------------thread num b77bdb70-----------------------2busy2---------------------thread num b5fbab70-----------------------2busy2-----------Thread b5fbab70 was canceled on its 1174527 try.Thread b77bdb70 was canceled on its 1023100 try.-----------i = 1--------------Thread b6fbcb70 was canceled on its 1174527 try.-----------i = 2-------------------------i = 3--------------It took 1174527 tries ot find the number!
從這結果里你有沒有看出什么呢?呵呵~.~
以上就是小編為大家帶來的linux線程的取消(終止)方法全部內容了,希望大家多多支持VEVB武林網~
新聞熱點
疑難解答