当前位置: 移动技术网 > 网络运营>服务器>Linux > linux线程的取消(终止)方法

linux线程的取消(终止)方法

2019年04月25日  | 移动技术网网络运营  | 我要评论

关键:

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 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) { 
//一个线程操作后其余线程进入次循环挂起,等待pthread_cancel函数发送cancel信号终止线程
k++;
if(k == 10000)
{
printf("----------2busy2-----------\n");
}

pthread_testcancel();
}
tries = ntries;
//pthread_mutex_unlock(&mutex);  //如果加上这句话,将会有好几个线程找到主函数中设定的值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函数发送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 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!

从这结果里你有没有看出什么呢?呵呵~.~

以上就是小编为大家带来的linux线程的取消(终止)方法全部内容了,希望大家多多支持移动技术网~

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网