织梦CMS - 轻松建站从此开始!

罗索

使用 pthread_testcancel 作为pthread_cancel 时 线程的退出点函

落鹤生 发布于 2011-12-23 10:57 点击:次 
使用 pthread_testcancel 作为pthread_cancel 时 线程的退出点函数
TAG:

pthread_testcancel()--Create Cancellation Point

Syntax

#include <pthread.h>

void pthread_testcancel(void);

Threadsafe: Yes Signal Safe: No

The

pthread_testcancel

() function creates a cancellation point in the calling thread. If cancelability is currently disabled, this function has no effect. For more information on cancelability, see Thread cancellation APIs.

When cancelability is disabled, all cancels are held pending in the target thread until the thread changes the cancelability. When cancelability is deferred, all cancels are held pending in the target thread until the thread changes the cancelability, calls a function that is a cancellation point, or calls pthread_testcancel(), thus creating a cancellation point. When cancelability is asynchronous, all cancels are acted upon immediately, interrupting the thread with its processing.

You should not use asynchronous thread cancellation via the PTHREAD_CANCEL_ASYNCHRONOUS option of pthread_setcanceltype(). See the common user errors section of this document for more information.

  1. #define _MULTI_THREADED 
  2. #include <pthread.h> 
  3. #include <stdio.h> 
  4. #include "check.h" 
  5.  
  6. void cleanupHandler(void *parm) { 
  7.   printf("Inside cancellation cleanup handler/n"); 
  8.  
  9. void *threadfunc(void *parm) 
  10.   unsigned int  i=0; 
  11.   int           rc=0, oldState=0; 
  12.   printf("Entered secondary thread/n"); 
  13.   pthread_cleanup_push(cleanupHandler, NULL); 
  14.   rc = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState); 
  15.   checkResults("pthread_setcancelstate()/n", rc); 
  16.   /* Allow cancel to be pending on this thread */ 
  17.   sleep(2); 
  18.   while (1) { 
  19.     printf("Secondary thread is now looping/n"); 
  20.     ++i; 
  21.     sleep(1); 
  22.     /* pthread_testcancel() has no effect until cancelability is enabled.*/ 
  23.     /* At that time, a call to pthread_testcancel() should result in the */ 
  24.     /* pending cancel being acted upon                                   */ 
  25.     pthread_testcancel(); 
  26.     if (i == 5) { 
  27.       printf("Cancel state set to ENABLE/n"); 
  28.       rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldState); 
  29.       checkResults("pthread_setcancelstate(2)/n", rc); 
  30.       /* Now, cancellation points will allow pending cancels 
  31.          to get through to this thread */ 
  32.     } 
  33.   } /* infinite */ 
  34.   pthread_cleanup_pop(0); 
  35.   return NULL; 
  36.  
  37. int main(int argc, char **argv) 
  38.   pthread_t             thread
  39.   int                   rc=0; 
  40.   void                 *status=NULL; 
  41.  
  42.   printf("Enter Testcase - %s/n", argv[0]); 
  43.  
  44.   /* Create a thread using default attributes */ 
  45.   printf("Create thread using the NULL attributes/n"); 
  46.   rc = pthread_create(&thread, NULL, threadfunc, NULL); 
  47.   checkResults("pthread_create(NULL)/n", rc); 
  48.  
  49.   sleep(1); 
  50.   printf("Cancel the thread/n"); 
  51.   rc = pthread_cancel(thread); 
  52.   checkResults("pthread_cancel()/n", rc); 
  53.  
  54.   rc = pthread_join(thread, &status); 
  55.   if (status != PTHREAD_CANCELED) { 
  56.     printf("Thread returned unexpected result!/n"); 
  57.     exit(1); 
  58.   } 
  59.   printf("Main completed/n"); 
  60.   return 0; 

Output:

Enter Testcase - QP0WTEST/TPTESTC0
Create thread using the NULL attributes
Entered secondary thread
Cancel the thread
Secondary thread is now looping
Secondary thread is now looping
Secondary thread is now looping
Secondary thread is now looping
Secondary thread is now looping
Cancel state set to ENABLE
Secondary thread is now looping
Inside cancellation cleanup handler
Main completed

(stevenliyong)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201112/15522.html]
本文出处:CSDN博客 作者:stevenliyong 原文
顶一下
(1)
50%
踩一下
(1)
50%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容