linux線程的取消(終止)方法
關(guān)鍵:
pthread_cancel函數(shù)發(fā)送終止信號
pthread_setcancelstate函數(shù)設(shè)置終止方式
pthread_testcancel函數(shù)取消線程(另一功能是:設(shè)置取消點(diǎn))
1 線程取消的定義
一般情況下,線程在其主體函數(shù)退出的時(shí)候會(huì)自動(dòng)終止,但同時(shí)也可以因?yàn)榻邮盏搅硪粋€(gè)線程發(fā)來的終止(取消)請求而強(qiáng)制終止。
2 線程取消的語義
線程取消的方法是向目標(biāo)線程發(fā)Cancel信號(pthread_cancel函數(shù)發(fā)送Cancel信號),但如何處理Cancel信號則由目標(biāo)線程自己決定,或者忽略、或者立即終止、或者繼續(xù)運(yùn)行至Cancelation-point(取消點(diǎn)),由不同的Cancelation狀態(tài)(pthread_setcancelstate函數(shù)設(shè)置狀態(tài))決定。
線程接收到CANCEL信號的缺省處理(即pthread_create()創(chuàng)建線程的缺省狀態(tài))是繼續(xù)運(yùn)行至取消點(diǎn),也就是說設(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的手冊頁聲稱,由于LinuxThread庫與C庫結(jié)合得不好,因而目前C庫函數(shù)都不是Cancelation-point;但CANCEL信號會(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ì)方面的考慮
如果線程處于無限循環(huán)中,且循環(huán)體內(nèi)沒有執(zhí)行至取消點(diǎn)的必然路徑,則線程無法由外部其他線程的取消請求而終止。因此在這樣的循環(huán)體的必經(jīng)路徑上應(yīng)該加入pthread_testcancel()調(diào)用。
5 與線程取消相關(guān)的pthread函數(shù)
int pthread_cancel(pthread_t thread)
發(fā)送終止信號給thread線程,如果成功則返回0,否則為非0值。發(fā)送成功并不意味著thread會(huì)終止。
int pthread_setcancelstate(int state, int *oldstate)
設(shè)置本線程對Cancel信號的反應(yīng),state有兩種值:PTHREAD_CANCEL_ENABLE(缺?。┖?PTHREAD_CANCEL_DISABLE,分別表示收到信號后設(shè)為CANCLED狀態(tài)和忽略CANCEL信號繼續(xù)運(yùn)行;old_state如果不為 NULL則存入原來的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í)有效,分別表示收到信號后繼續(xù)運(yùn)行至下一個(gè)取消點(diǎn)再退出和立即執(zhí)行取消動(dòng)作(退出);oldtype如果不為NULL則存入運(yùn)來的取消動(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信號終止線程
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++;
/*線程阻塞,其他線程爭奪資源,或者是等待pthread_cancel函數(shù)發(fā)送cancel信號終止線程*/
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é)果里你有沒有看出什么呢?呵呵~.~
以上就是小編為大家?guī)淼膌inux線程的取消(終止)方法全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
總結(jié)Centos7系統(tǒng)加固知識(shí)點(diǎn)
本篇文章給大家詳細(xì)介紹了LINUX中Centos7系統(tǒng)加固的相關(guān)知識(shí)點(diǎn),如果大家對此有需要跟著學(xué)習(xí)下吧。2018-02-02
詳解如何在 CentOS 7 中添加新磁盤而不用重啟系統(tǒng)
本篇文章主要介紹了詳解如何在 CentOS 7 中添加新磁盤而不用重啟系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
通過配置.htaccess文件實(shí)現(xiàn)子目錄綁定二級域名的方法
.htaccess文件 子目錄綁定二級域名2010-02-02
Nginx啟動(dòng)SSL功能,并進(jìn)行功能優(yōu)化詳細(xì)介紹
這篇文章主要介紹了Nginx啟動(dòng)SSL功能,并進(jìn)行功能優(yōu)化詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12
Linux中怎么通過PID號找到對應(yīng)的進(jìn)程名及所在目錄方法
本篇文章主要介紹了Linux中怎么通過PID號找到對應(yīng)的進(jìn)程名及所在目錄方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
CentOS 5.5使用yum來安裝LAMP(php運(yùn)行環(huán)境)
今天用yum方法搭建起了個(gè)LAMP環(huán)境,中間遇到了很多問題,經(jīng)過google和各位前輩的幫助,終于將環(huán)境搭建起來,現(xiàn)在把完整的步驟記錄下來,2010-09-09
CentOS7安裝PHP7 Redis擴(kuò)展的方法步驟
這篇文章主要介紹了CentOS7安裝PHP7 Redis擴(kuò)展的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04

