pthread_cancel
#include <pthread.h>
#include <stdio.h>
#include<stdlib.h>
#include <unistd.h> static void checkResults(char *string, int rc) {
if (rc) {
printf("Error on : %s, rc=%d",
string, rc);
exit(EXIT_FAILURE);
}
return;
} void *threadfunc(void *parm)
{
printf("Entered secondary thread\n");
int cnt = ;
while () {
printf("Secondary thread is looping, %d\n", cnt);
cnt ++;
pthread_testcancel();
sleep();
}
return NULL;
} int main(int argc, char **argv)
{
pthread_t thread;
int rc=;
void *thread_result; printf("Entering testcase\n"); /* Create a thread using default attributes */
printf("Create thread using the NULL attributes\n");
rc = pthread_create(&thread, NULL, threadfunc, NULL);
checkResults("pthread_create(NULL)\n", rc); /* sleep() is not a very robust way to wait for the thread */
sleep(); printf("Cancel the thread\n");
rc = pthread_cancel(thread);
checkResults("pthread_cancel()\n", rc); rc = pthread_join(thread, &thread_result);
if (thread_result == PTHREAD_CANCELED)
printf(" return PTHREAD_CANCELED\n"); /* sleep() is not a very robust way to wait for the thread */
sleep();
printf("Main completed\n");
return ;
}
参考
http://www.cnblogs.com/Creator/archive/2012/03/21/2408413.html
pthread_cancel可以单独使用,因为在很多系统函数里面本身就有很多的取消点断点,当调用这些系统函数时就会命中其内部的断点来结束线程,如下面的代码中,即便注释掉我们自己设置的断点pthread_testcancel()程序还是一样的会被成功的cancel掉,因为printf函数内部有取消点(如果大家想了解更多的函数的取消点情况,可以阅读《Unix高级环境编程》的线程部分)
上班的代码去掉pthread_testcancel()和print程序还是一样的会被成功的cancel掉,因为sleep本身内部也有取消点吧。
当把上述程序中while(1)中删除sleep()和print函数以及pthread_testcancel()后,线程无法被取消。。证明while(1)实现没有取消点。
pthread_testcancel() 主要针对那种计算密集型的
描述:函数在运行的线程中创建一个取消点,如果cancellation无效则此函数不起作用。
pthread的建议是:如果一个函数是阻塞的,那么你必须在这个函数前后建立 “ 取消点 ”, 比如:
printf("sleep\n");
pthread_testcancel();
sleep(10);
pthread_testcancel();
printf("wake \n");
在执行到pthread_testcancel的位置时,线程才可能响应cancel退出进程。
对于一些函数来说本身就是有cancellation point 的,那么可以不管,但是大部分还是没有的,
所以要使用pthread_testcancel来设置一个取消点,那么也并不是对于所有的函数都是有效的,
对于有延时的函数才是有效的,更清楚的说是有时间让pthread_cancel响应才是OK的!
#include <pthread.h>
#include <stdio.h>
#include<stdlib.h>
#include <unistd.h> #include <pthread.h>
void testPointerSize()
{
void *tret;
printf("size of pointer in x86-64:%d\n",sizeof(tret));
//result is 8 in x86-64.
//which is 4 in x86-32. printf("size of int in x86-64:%d\n",sizeof(int));
//result is 4 in x86-64.
//which is also 4 in x86-32.
}
void cleanup(void *arg)
{
printf("cleanup:%s\n",(char *)arg);
} void * thr_fn4(void *arg)
{
printf("thread 4 start\n");
pthread_cleanup_push(cleanup, "thread 4 first handler");
pthread_cleanup_push(cleanup, "thread 4 second handler");
pthread_cleanup_push(cleanup, "thread 4 3th handler"); while()
{
usleep();
} pthread_cleanup_pop();
pthread_cleanup_pop();
pthread_cleanup_pop();
pthread_exit((void *));
//pthread_exit() here will triger cleanup second handler.
} int main(void)
{
testPointerSize();
int err;
pthread_t tid1, tid2, tid3, tid4;
void *tret; err = pthread_create(&tid4, NULL, thr_fn4, (void *));
sleep();
pthread_cancel(tid4);
err = pthread_join(tid4, &tret);
printf("thread 4 exit code %d\n",(int)tret);
if(tret == PTHREAD_CANCELED)
printf("thread is cancelled\n"); return ;
}
输出:
size of pointer in x86-:
size of int in x86-:
thread start
cleanup:thread 3th handler
cleanup:thread second handler
cleanup:thread first handler
thread exit code -
thread is cancelled
可见,PTHREAD_CANCELED 被定义成-1.
基本概念
pthread_cancel调用并不等待线程终止,它只提出请求。线程在取消请求(pthread_cancel)发出后会继续运行,
直到到达某个取消点(CancellationPoint)。取消点是线程检查是否被取消并按照请求进行动作的一个位置.
与线程取消相关的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)
是说pthread_testcancel在不包含取消点,但是又需要取消点的地方创建一个取消点,以便在一个没有包含取消点的执行代码线程中响应取消请求.
线程取消功能处于启用状态且取消状态设置为延迟状态时,pthread_testcancel()函数有效。
如果在取消功能处处于禁用状态下调用pthread_testcancel(),则该函数不起作用。
请务必仅在线程取消线程操作安全的序列中插入pthread_testcancel()。除通过pthread_testcancel()调用以编程方式建立的取消点意外,pthread标准还指定了几个取消点。测试退出点,就是测试cancel信号.
取消点:
线程取消的方法是向目标线程发Cancel信号,但如何处理Cancel信号则由目标线程自己决定,或者忽略、或者立即终止、或者继续运行至Cancelation-point(取消点),由不同的Cancelation状态决定。
线程接收到CANCEL信号的缺省处理(即pthread_create()创建线程的缺省状态)是继续运行至取消点,也就是说设置一个CANCELED状态,线程继续运行,只有运行至Cancelation-point的时候才会退出。
pthreads标准指定了几个取消点,其中包括:
(1)通过pthread_testcancel调用以编程方式建立线程取消点。
(2)线程等待pthread_cond_wait或pthread_cond_timewait()中的特定条件。
(3)被sigwait(2)阻塞的函数
(4)一些标准的库调用。通常,这些调用包括线程可基于阻塞的函数。
缺省情况下,将启用取消功能。有时,您可能希望应用程序禁用取消功能。如果禁用取消功能,则会导致延迟所有的取消请求,
直到再次启用取消请求。
根据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();
注意:
程序设计方面的考虑,如果线程处于无限循环中,且循环体内没有执行至取消点的必然路径,则线程无法由外部其他线程的取消请求而终止。因此在这样的循环体的必经路径上应该加入pthread_testcancel()调用.
取消类型(Cancellation Type)
我们会发现,通常的说法:某某函数是 Cancellation Points,这种方法是容易令人混淆的。
因为函数的执行是一个时间过程,而不是一个时间点。其实真正的 Cancellation Points 只是在这些函数中 Cancellation Type 被修改为 PHREAD_CANCEL_ASYNCHRONOUS 和修改回 PTHREAD_CANCEL_DEFERRED 中间的一段时间。
POSIX的取消类型有两种,一种是延迟取消(PTHREAD_CANCEL_DEFERRED),这是系统默认的取消类型,即在线程到达取消点之前,不会出现真正的取消;另外一种是异步取消(PHREAD_CANCEL_ASYNCHRONOUS),使用异步取消时,线程可以在任意时间取消。
http://www.cnblogs.com/mydomain/archive/2011/08/15/2139830.html
一个 pthread_cancel 引起的线程死锁【整理转载】
pthread_cancel的更多相关文章
- Linux多线程实例练习 - pthread_cancel()
Linux多线程实例练习 - pthread_cancel 1.代码 xx_pthread_cancel.c #include <pthread.h> #include <stdio ...
- 线程取消 (pthread_cancel)
线程取消(pthread_cancel) 基本概念pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(C ...
- 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程
一. pthread_create() #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_a ...
- pthread_cleanup_push与pthread_cleanup_pop与pthread_cancel与pthread_testcancel
参考: http://blog.csdn.net/zjc156m/article/details/9021343 http://blog.csdn.net/u010027547/article/det ...
- 线程正常终止pthread_exit,pthread_join,pthread_kill,pthread_cancel,sigwait,sigaddset
int pthread_join(pthread_t thread, void **retval); int pthread_detach(pthread_t thread); void pthrea ...
- 如何不使用pthread_cancel而杀死线程
http://www.cnblogs.com/no7dw/archive/2012/09/27/2705847.html During the time I use standalone cross ...
- pthread_testcancel和pthread_cancel函数的简单示例
/*0.取消线程 int pthread_cancel(pthread_t thread); 设置取消点 void pthread_testcancel(void); 测试是否接收到取消请求,如果有, ...
- linux下pthread_cancel无法取消线程的原因【转】
转自:http://blog.csdn.net/huangshanchun/article/details/47420961 版权声明:欢迎转载,如有不足之处,恳请斧正. 一个线程可以调用pthrea ...
- pthread_cancel函数注意事项
/************************************************** 相关函数: #include <pthread.h> int pthread_can ...
随机推荐
- python大数据工作流程
本文作者:hhh5460 大数据分析,内存不够用怎么办? 当然,你可以升级你的电脑为超级电脑. 另外,你也可以采用硬盘操作. 本文示范了硬盘操作的一种可能的方式. 本文基于:win10(64) + p ...
- 敏捷软件开发 Agile software Development(转)
原文链接: http://www.cnblogs.com/kkun/archive/2011/07/06/2099253.html 敏捷软件开发 Agile software Development ...
- freemarker语法简介
ftl是一种模板标记语言,用于渲染数据,输入html结构.语法简介如下: ${book.name} ${book.name?if_exists} //值是否存在 ${book.name??} //值是 ...
- [CareerCup] 12.5 Test a Pen 测试一支笔
12.5 How would you testa pen? 这道题让我们测试一支笔,我们需要问面试官许多问题来理解"who,what,where,when,how and why" ...
- Bootstrap系列 -- 32. 按钮垂直分组
实际运用当中,总会碰到垂直显示的效果.在Bootstrap框架中也提供了这样的风格.我们只需要把水平分组的“btn-group”类名换成“btn-group-vertical”即可. <div ...
- [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)
http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...
- 2、面向对象以及winform的简单运用(面向对象的四大基本特性)
面向对象的四大基本特性 面向对象的核心概念就是封装.抽象.继承.多态这四大基本特性,在这里先解释一下它们的定义: 封装:是一种隐藏信息的特性.找到变化并且把它封装起来,你就可以在不影响其它部分的情况下 ...
- 第三十一课:JSDeferred详解2
这一课,我们先接着上一课讲一下wait方法,以及wait方法是如何从静态方法变化实例方法的. 首先我们先看wait方法为啥可以从静态方法变成实例方法,请看register源码: Deferred.re ...
- NABCD分析java音乐播放器
程设计题目:java音乐播放器 一.课程设计目的 1.编程设计音乐播放软件,使之实现音乐播放的功能. 2.培养学生用程序解决实际问题的能力和兴趣. 3.加深java中对多媒体编程的应用. 二.课程设计 ...
- javaee添加验证码
package com.zh.code; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import j ...