引自http://www.cnblogs.com/xiedan/archive/2009/12/16/1625977.html
线程取消
2.1 线程取消的定义
一般情况下,线程在其主体函数退出的时候会自动终止,但同时也可以因为接收到另一个线程发来的终止(取消)请求而强制终止。
2.2 线程取消的语义
线程取消的方法是向目标线程发Cancel信号,但如何处理Cancel信号则由目标线程自己决定,或者忽略、或者立即终止、或者继续运行至Cancelation-point(取消点),由不同的Cancelation状态决定。
线程接收到CANCEL信号的缺省处理(即pthread_create()创建线程的缺省状态)是继续运行至取消点,也就是说设置一个CANCELED状态,线程继续运行,只有运行至Cancelation-point的时候才会退出。
2.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();2.4 程序设计方面的考虑
如果线程处于无限循环中,且循环体内没有执行至取消点的必然路径,则线程无法由外部其他线程的取消请求而终止。因此在这样的循环体的必经路径上应该加入pthread_testcancel()调用。
2.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<iostream>
#include<unistd.h>
#include<pthread.h>
usingnamespacestd;
pthread_tpid[3];
void*thread_run_1(void*arg){
cout<<"Now in the thread 1"<<endl;
intsum=0;
intstate,oldstate;
state=PTHREAD_CANCEL_DEFERRED;
pthread_setcancelstate(state,&oldstate);
cout<<"oldstate is "<<(state==oldstate?"Deferred":"Async")<<endl;
//耗时间的循环
for(inti=1;i<=INT_MAX;++i);
cout<<"before testcancel"<<endl;
pthread_testcancel();
cout<<"after testcancel"<<endl;
cout<<"thread 1 done!"<<endl;
}
void*thread_run_2(void*arg){
cout<<"Now in the thread 2"<<endl;
pthread_cancel(pid[1]);
sleep(2);
cout<<"thread 2 done!"<<endl;
}
intmain(){
pid[0]=pthread_self();
if(pthread_create(&pid[1],NULL,thread_run_1,NULL)!=0){
cout<<"error create thread 1"<<endl;
return-1;
}
if(pthread_create(&pid[2],NULL,thread_run_2,NULL)!=0){
cout<<"error create thread 2"<<endl;
return-1;
}
sleep(5);
cout<<"Main thread done!"<<endl;
}
在这个程序中,我们在main线程中生成两个线程:thread1和thread2,并使用一个全局数组pid来保存线程的id。在这个程序中,我们在thread2中执行取消其他线程的操作。
运行结果如下:
[antony@localhost src]$ g++ cancel_thread.cpp -lpthread
[antony@localhost src]$ ./a.out
Now in the thread 1
oldstate is Deferred
Now in the thread 2
thread 2 done!
Main thread done!
[antony@localhost src]$
thread1后面几个输出都没有进行,可以看到是确实的被thread2取消了。
下面进行进一步的讨论:
1、很明显,thread1并没有运行到pthread_testcancel所指示的地方,我估计上是在cout函数中存在取消点。可以增加thread1的循环次数,例如:
cout<<"before testcancel"<<endl;
for(inti=0;i<INT_MAX;++i)
for(intj=0;j<INT_MAX;++j);
运行结果并没有什么不同,经测试pthread_cancel()函数也没有返回出错值。因此上,我们可以知道,pthread_cancel函数是非阻塞函数,这个程序运行的过程中应该发生了这样的事情:thread2提请内核cancel掉thread1,然后thread2自己结束返回,之后经过一段时间的调度,main thread得到了运行机会,输出Main thread done!后mainthread结束,然后整个进程结束,thread1随着整个进程被杀掉。
实际上,我们在这个测试中并没有测试到pthread_testcancel函数,如果我们把mainthread中sleep的时间增加,就可以使得程序运行到thread1的pthread_testcancel()处。如果thread1确实被取消了,则“after testcancel”不会被输出。
为了在main thread中确保thread1返回,我们使用pthread_join函数来阻塞整个程序直到thread1返回,这个函数的用法稍后继续说明。
在main函数中修改下列语句:
pthread_join(pid[1],NULL);
这样,main函数的结束标志着thread1必然已经结束。为了使得等待的时间不要太长(循环INT_MAX次足够你睡一个午觉了)建议把thread1中的循环次数改小一点,具体的根据你的机器而定,只要运行时间超过2-3秒就足够了,我把它设置成大约1e9。
编译运行结果:
[antony@localhost src]$ g++ cancel_thread.cpp -lpthread
[antony@localhost src]$ ./a.out
Now in the thread 1
oldstate is Deferred
before testcancel
Now in the thread 2
thread 2 done!
Main thread done!
[antony@localhost src]$
这样的结果,我们可以看到,thread1运行到pthread_testcancel处被取消而返回main线程,从而后面的输出都没有完成。
2、可能有人奇怪为什么main函数中要有sleep(5),不妨去掉这行代码运行一下:
编译运行结果:
[antony@localhost src]$ g++ cancel_thread.cpp -lpthread
[antony@localhost src]$ ./a.out
Main thread done!
[antony@localhost src]$
其他线程还没有得到运行机会就终止了,因为main thread结束之后,整个进程也就结束了,并不等待所有线程都完成。
3、如果我们取消main线程,会发生什么事情呢?
把thread2中的pthread_cancel函数的参数稍微改一下,改成pid[0],编译运行的结果如下:
[antony@localhost src]$ g++ cancel_thread.cpp -lpthread
[antony@localhost src]$ ./a.out
Now in the thread 1
oldstate is Deferred
before testcancel
Now in the thread 2
thread 2 done!
after testcancel
thread 1 done!
[antony@localhost src]$
main线程被我们顺利取消了!在thread1运行结束之后,整个进程结束。
如果我们在取消main线程之前,取消掉thread1,也就是:
pthread_cancel(pid[1]);
pthread_cancel(pid[0]);
编译运行结果如下:
[antony@localhost src]$ g++ cancel_thread.cpp -lpthread
[antony@localhost src]$ ./a.out
Now in the thread 1
oldstate is Deferred
before testcancel
Now in the thread 2
thread 2 done!
[antony@localhost src]$
需要说明的是,在thread2结束之后,整个进程并没有马上结束,而是等到thread1运行到pthread_cancel之后才结束的。取消thread1的命令并没有随着main线程的结束而结束。
总结一下这个对这个实例的研究:
pthread_cancel()函数并不阻塞,在向内核发出取消某个线程的信号之后立刻返回调用线程。
pthread_cancel()的信号并不会随着main线程状态的改变而改变,这个信号是直接与内核相连的。
对于一个多线程程序而言,以下两种情况都会导致其终止:
a) main线程正常结束,则进程不管其他线程的情况,马上结束(也有可能是给其他线程发了终止信号,这个问题留待我研究信号的时候进一步讨论)
b) main线程被其他线程取消,则等待所有线程结束(关于“所有”,我增加了一个thread3测试过了。)