欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux線程的取消(終止)方法

 更新時間:2017年01月05日 08:53:13   投稿:jingxian  
下面小編就為大家?guī)硪黄猯inux線程的取消(終止)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

關鍵:

pthread_cancel函數(shù)發(fā)送終止信號
pthread_setcancelstate函數(shù)設置終止方式
pthread_testcancel函數(shù)取消線程(另一功能是:設置取消點)

1 線程取消的定義

一般情況下,線程在其主體函數(shù)退出的時候會自動終止,但同時也可以因為接收到另一個線程發(fā)來的終止(取消)請求而強制終止。

2 線程取消的語義

線程取消的方法是向目標線程發(fā)Cancel信號(pthread_cancel函數(shù)發(fā)送Cancel信號),但如何處理Cancel信號則由目標線程自己決定,或者忽略、或者立即終止、或者繼續(xù)運行至Cancelation-point(取消點),由不同的Cancelation狀態(tài)(pthread_setcancelstate函數(shù)設置狀態(tài))決定。

線程接收到CANCEL信號的缺省處理(即pthread_create()創(chuàng)建線程的缺省狀態(tài))是繼續(xù)運行至取消點,也就是說設置一個CANCELED狀態(tài),線程繼續(xù)運行,只有運行至Cancelation-point的時候才會退出。

3 取消點

根據(jù)POSIX標準,pthread_join()、pthread_testcancel()、pthread_cond_wait()、 pthread_cond_timedwait()、sem_wait()、sigwait()等函數(shù)以及read()、write()等會引起阻塞的系統(tǒng)調(diào)用都是Cancelation-point,而其他pthread函數(shù)都不會引起Cancelation動作。但是pthread_cancel的手冊頁聲稱,由于LinuxThread庫與C庫結(jié)合得不好,因而目前C庫函數(shù)都不是Cancelation-point;但CANCEL信號會使線程從阻塞的系統(tǒng)調(diào)用中退出,并置EINTR錯誤碼,因此可以在需要作為Cancelation-point的系統(tǒng)調(diào)用前后調(diào)用 pthread_testcancel(),從而達到POSIX標準所要求的目標,即如下代碼段:

pthread_testcancel();
retcode = read(fd, buffer, length);
 pthread_testcancel();

4 程序設計方面的考慮

如果線程處于無限循環(huán)中,且循環(huán)體內(nèi)沒有執(zhí)行至取消點的必然路徑,則線程無法由外部其他線程的取消請求而終止。因此在這樣的循環(huán)體的必經(jīng)路徑上應該加入pthread_testcancel()調(diào)用。

5 與線程取消相關的pthread函數(shù)

int pthread_cancel(pthread_t thread)

發(fā)送終止信號給thread線程,如果成功則返回0,否則為非0值。發(fā)送成功并不意味著thread會終止。

int pthread_setcancelstate(int state, int *oldstate)

設置本線程對Cancel信號的反應,state有兩種值:PTHREAD_CANCEL_ENABLE(缺?。┖?PTHREAD_CANCEL_DISABLE,分別表示收到信號后設為CANCLED狀態(tài)和忽略CANCEL信號繼續(xù)運行;old_state如果不為 NULL則存入原來的Cancel狀態(tài)以便恢復。

int pthread_setcanceltype(int type, int *oldtype)

設置本線程取消動作的執(zhí)行時機,type由兩種取值:PTHREAD_CANCEL_DEFFERED和 PTHREAD_CANCEL_ASYCHRONOUS,僅當Cancel狀態(tài)為Enable時有效,分別表示收到信號后繼續(xù)運行至下一個取消點再退出和立即執(zhí)行取消動作(退出);oldtype如果不為NULL則存入運來的取消動作類型值。

void pthread_testcancel(void)

功能一:設置取消點;

功能二:檢查本線程是否處于Canceld狀態(tài),如果是,則進行取消動作,否則直接返回。

代碼:

#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)
{
//只允許一個線程操作此處
while(pthread_mutex_trylock(&mutex) == EBUSY) { 
//一個線程操作后其余線程進入次循環(huán)掛起,等待pthread_cancel函數(shù)發(fā)送cancel信號終止線程
k++;
if(k == 10000)
{
printf("----------2busy2-----------\n");
}

pthread_testcancel();
}
tries = ntries;
//pthread_mutex_unlock(&mutex);  //如果加上這句話,將會有好幾個線程找到主函數(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ù)的作用而設置的代碼段*/
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ù)

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;
}

運行結(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)容了,希望大家多多支持腳本之家~

相關文章

最新評論