linux線程的取消(終止)方法
關(guān)鍵:
pthread_cancel函數(shù)發(fā)送終止信號(hào)
pthread_setcancelstate函數(shù)設(shè)置終止方式
pthread_testcancel函數(shù)取消線程(另一功能是:設(shè)置取消點(diǎn))
1 線程取消的定義
一般情況下,線程在其主體函數(shù)退出的時(shí)候會(huì)自動(dòng)終止,但同時(shí)也可以因?yàn)榻邮盏搅硪粋€(gè)線程發(fā)來(lái)的終止(取消)請(qǐng)求而強(qiáng)制終止。
2 線程取消的語(yǔ)義
線程取消的方法是向目標(biāo)線程發(fā)Cancel信號(hào)(pthread_cancel函數(shù)發(fā)送Cancel信號(hào)),但如何處理Cancel信號(hào)則由目標(biāo)線程自己決定,或者忽略、或者立即終止、或者繼續(xù)運(yùn)行至Cancelation-point(取消點(diǎn)),由不同的Cancelation狀態(tài)(pthread_setcancelstate函數(shù)設(shè)置狀態(tài))決定。
線程接收到CANCEL信號(hào)的缺省處理(即pthread_create()創(chuàng)建線程的缺省狀態(tài))是繼續(xù)運(yùn)行至取消點(diǎn),也就是說(shuō)設(shè)置一個(gè)CANCELED狀態(tài),線程繼續(xù)運(yùn)行,只有運(yùn)行至Cancelation-point的時(shí)候才會(huì)退出。
3 取消點(diǎn)
根據(jù)POSIX標(biāo)準(zhǔn),pthread_join()、pthread_testcancel()、pthread_cond_wait()、 pthread_cond_timedwait()、sem_wait()、sigwait()等函數(shù)以及read()、write()等會(huì)引起阻塞的系統(tǒng)調(diào)用都是Cancelation-point,而其他pthread函數(shù)都不會(huì)引起Cancelation動(dòng)作。但是pthread_cancel的手冊(cè)頁(yè)聲稱,由于LinuxThread庫(kù)與C庫(kù)結(jié)合得不好,因而目前C庫(kù)函數(shù)都不是Cancelation-point;但CANCEL信號(hào)會(huì)使線程從阻塞的系統(tǒng)調(diào)用中退出,并置EINTR錯(cuò)誤碼,因此可以在需要作為Cancelation-point的系統(tǒng)調(diào)用前后調(diào)用 pthread_testcancel(),從而達(dá)到POSIX標(biāo)準(zhǔn)所要求的目標(biāo),即如下代碼段:
pthread_testcancel();
retcode = read(fd, buffer, length);
pthread_testcancel();
4 程序設(shè)計(jì)方面的考慮
如果線程處于無(wú)限循環(huán)中,且循環(huán)體內(nèi)沒(méi)有執(zhí)行至取消點(diǎn)的必然路徑,則線程無(wú)法由外部其他線程的取消請(qǐng)求而終止。因此在這樣的循環(huán)體的必經(jīng)路徑上應(yīng)該加入pthread_testcancel()調(diào)用。
5 與線程取消相關(guān)的pthread函數(shù)
int pthread_cancel(pthread_t thread)
發(fā)送終止信號(hào)給thread線程,如果成功則返回0,否則為非0值。發(fā)送成功并不意味著thread會(huì)終止。
int pthread_setcancelstate(int state, int *oldstate)
設(shè)置本線程對(duì)Cancel信號(hào)的反應(yīng),state有兩種值:PTHREAD_CANCEL_ENABLE(缺省)和 PTHREAD_CANCEL_DISABLE,分別表示收到信號(hào)后設(shè)為CANCLED狀態(tài)和忽略CANCEL信號(hào)繼續(xù)運(yùn)行;old_state如果不為 NULL則存入原來(lái)的Cancel狀態(tài)以便恢復(fù)。
int pthread_setcanceltype(int type, int *oldtype)
設(shè)置本線程取消動(dòng)作的執(zhí)行時(shí)機(jī),type由兩種取值:PTHREAD_CANCEL_DEFFERED和 PTHREAD_CANCEL_ASYCHRONOUS,僅當(dāng)Cancel狀態(tài)為Enable時(shí)有效,分別表示收到信號(hào)后繼續(xù)運(yùn)行至下一個(gè)取消點(diǎn)再退出和立即執(zhí)行取消動(dòng)作(退出);oldtype如果不為NULL則存入運(yùn)來(lái)的取消動(dòng)作類型值。
void pthread_testcancel(void)
功能一:設(shè)置取消點(diǎn);
功能二:檢查本線程是否處于Canceld狀態(tài),如果是,則進(jìn)行取消動(dòng)作,否則直接返回。
代碼:
#include <stdio.h> #include <errno.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> #define THREAD_MAX 4 pthread_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) { //只允許一個(gè)線程操作此處 while(pthread_mutex_trylock(&mutex) == EBUSY) { //一個(gè)線程操作后其余線程進(jìn)入次循環(huán)掛起,等待pthread_cancel函數(shù)發(fā)送cancel信號(hào)終止線程 k++; if(k == 10000) { printf("----------2busy2-----------\n"); } pthread_testcancel(); } tries = ntries; //pthread_mutex_unlock(&mutex); //如果加上這句話,將會(huì)有好幾個(gè)線程找到主函數(shù)中設(shè)定的值pid printf("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++; /*線程阻塞,其他線程爭(zhēng)奪資源,或者是等待pthread_cancel函數(shù)發(fā)送cancel信號(hào)終止線程*/ pthread_testcancel(); /*這是為了弄明白pthread_testcancel函數(shù)的作用而設(shè)置的代碼段*/ 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(); //設(shè)置要查找的數(shù) 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; }
運(yùn)行結(jié)果:
Search the num of 6531 -----------i = 0-------------- thread num b6fbcb70 thread num b67bbb70 thread num b5fbab70 thread 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!
從這結(jié)果里你有沒(méi)有看出什么呢?呵呵~.~
以上就是小編為大家?guī)?lái)的linux線程的取消(終止)方法全部?jī)?nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
Linux中如何修改~/.bashrc或/etc/profile設(shè)置環(huán)境變量
這篇文章主要介紹了Linux中如何修改~/.bashrc或/etc/profile設(shè)置環(huán)境變量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Ubuntu18.04一次性升級(jí)Python所有庫(kù)的方法步驟
這篇文章主要介紹了Ubuntu18.04一次性升級(jí)Python所有庫(kù)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Ubuntu編譯內(nèi)核模塊,內(nèi)容體現(xiàn)系統(tǒng)日志中
大家好,本篇文章主要講的是Ubuntu編譯內(nèi)核模塊,內(nèi)容體現(xiàn)系統(tǒng)日志中,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Linux驅(qū)動(dòng)之platform總線詳解
大家好,本篇文章主要講的是Linux驅(qū)動(dòng)之platform總線詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Linux centos下設(shè)置定時(shí)備份任務(wù)的方法步驟
這篇文章主要介紹了Linux centos下設(shè)置定時(shí)備份任務(wù)的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Linux下大量SYN_SENT連接問(wèn)題的解決方法
這篇文章主要介紹了Linux下大量SYN_SENT連接問(wèn)題的解決方法,需要的朋友可以參考下2015-01-01Linux編譯LVGL仿真器出錯(cuò)問(wèn)題解決
大家好,本篇文章主要講的是Linux編譯LVGL仿真器出錯(cuò)問(wèn)題解決,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01