码迷,mamicode.com
首页 > 其他好文 > 详细

pthread_cancel

时间:2014-07-31 16:50:06      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   strong   io   for   

 1 #include <pthread.h>
 2 #include <stdio.h>
 3 #include<stdlib.h>
 4 #include <unistd.h>
 5 
 6 static void checkResults(char *string, int rc) {
 7     if (rc) {
 8         printf("Error on : %s, rc=%d",
 9                 string, rc);
10         exit(EXIT_FAILURE);
11     }   
12     return;
13 }
14 
15 void *threadfunc(void *parm)
16 {
17     printf("Entered secondary thread\n");
18     int cnt = 0;
19     while (1) {
20         printf("Secondary thread is looping, %d\n", cnt);
21         cnt ++; 
22         pthread_testcancel();
23         sleep(1);
24     }   
25     return NULL;
26 }
27 
28 int main(int argc, char **argv)
29 {
30     pthread_t             thread;
31     int                   rc=0;
32     void                  *thread_result;
33 
34     printf("Entering testcase\n");
35 
36     /* Create a thread using default attributes */
37     printf("Create thread using the NULL attributes\n");
38     rc = pthread_create(&thread, NULL, threadfunc, NULL);
39     checkResults("pthread_create(NULL)\n", rc);
40 
41     /* sleep() is not a very robust way to wait for the thread */
42     sleep(4);
43 
44     printf("Cancel the thread\n");
45     rc = pthread_cancel(thread);
46     checkResults("pthread_cancel()\n", rc);
47 
48     rc = pthread_join(thread, &thread_result);
49     if (thread_result == PTHREAD_CANCELED)
50         printf(" return PTHREAD_CANCELED\n");
51 
52     /* sleep() is not a very robust way to wait for the thread */
53     sleep(3);
54     printf("Main completed\n");
55     return 0;
56 }

 

参考

 

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的!

 

pthread_cancel,布布扣,bubuko.com

pthread_cancel

标签:style   blog   http   color   使用   strong   io   for   

原文地址:http://www.cnblogs.com/diegodu/p/3880972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!