#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的更多相关文章

  1. Linux多线程实例练习 - pthread_cancel()

    Linux多线程实例练习 - pthread_cancel 1.代码 xx_pthread_cancel.c #include <pthread.h> #include <stdio ...

  2. 线程取消 (pthread_cancel)

    线程取消(pthread_cancel) 基本概念pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(C ...

  3. 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程

    一. pthread_create() #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_a ...

  4. 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 ...

  5. 线程正常终止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 ...

  6. 如何不使用pthread_cancel而杀死线程

    http://www.cnblogs.com/no7dw/archive/2012/09/27/2705847.html During the time I use standalone cross ...

  7. pthread_testcancel和pthread_cancel函数的简单示例

    /*0.取消线程 int pthread_cancel(pthread_t thread); 设置取消点 void pthread_testcancel(void); 测试是否接收到取消请求,如果有, ...

  8. linux下pthread_cancel无法取消线程的原因【转】

    转自:http://blog.csdn.net/huangshanchun/article/details/47420961 版权声明:欢迎转载,如有不足之处,恳请斧正. 一个线程可以调用pthrea ...

  9. pthread_cancel函数注意事项

    /************************************************** 相关函数: #include <pthread.h> int pthread_can ...

随机推荐

  1. 集成架构:对比 Web API 与面向服务的架构和企业应用程序集成(转)

    http://kb.cnblogs.com/page/521644/ 摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不 ...

  2. 【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布

    去年就知道有这个功能,不过没去深究总结过,最近有写网络博客的欲望了,于是又重新拾起这玩意儿. 具体到底是用Windows Live Writer 2012还是用Word 2013,个人觉得看个人,因为 ...

  3. 完美隐藏win7文件和文件夹

    有没有一种方法即使使用隐藏模式也不能查看, 没错可以用上帝模式....... 啥是Win7上帝模式?不知道的看看..... <<<<<<<<<&l ...

  4. myeclipse 2014新建maven web 项目步骤

    首先在myeclipse中配置maven的安装路径: 在pom.xml中加上: 1 2 3 4 5 6 7 8 9 10  <plugins>           <plugin&g ...

  5. LeetCode:Clone Graph

    题目如下:实现克隆图的算法  题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...

  6. 发布HTML5 RTS游戏-古代战争

    古代战争 游戏介绍 "古代战争"是一个2.5D即时战略游戏,使用了帝国时代2的素材,并参考了它的游戏设计和玩法. 游戏基于YEngine2D引擎开发,具备生产.建造.资源采集.战斗 ...

  7. 启动tomcat报错 Could not reserve enough space for object heap的解决办法

    问题:打开eclips启动tomcat发现报出Could not reserve enough space for object heap错误. 解决办法:1.首先检查tomcat是否能正常启动.re ...

  8. 手把手教你Linux服务器集群部署.net网站 - 让MVC网站运行起来

    一.Linux下面安装需要软件 我们这里需要安装的软件有: 1) Mono 3.2.8 : C#跨平台编译器,能使.Net运行与Linux下,目前.net 4.0可以完美运行在该平台下 2) ngin ...

  9. VC6.0中MFC界面换肤简例

    利用VC中的MFC进行界面设计时,发现界面上的各控件无法简易地进行调整,比如字体大小.颜色.格式等. 为了改变外观,小小地美化一下,今天决定动手一试. 网上提供的库和方法不计其数,我选择了SkinMa ...

  10. Koala Framework是什么?我为什么要写这个框架?

    当时的监管组,技术力量累积的很少,还在直连DB,使用着DataTable.DataSet作为数据的承载,监管是公司最近几年主推的项目,所以领导们决定进行重要调整. 初来乍到 由于之前没有任何的技术积累 ...