分享

使用 pthread_testcancel 线程的退出点函数

 mediatv 2014-04-03

 

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.

 

[c-sharp] view plaincopy
  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.   
  10. void *threadfunc(void *parm)  
  11. {  
  12.   unsigned int  i=0;  
  13.   int           rc=0, oldState=0;  
  14.   printf("Entered secondary thread/n");  
  15.   pthread_cleanup_push(cleanupHandler, NULL);  
  16.   rc = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);  
  17.   checkResults("pthread_setcancelstate()/n", rc);  
  18.   /* Allow cancel to be pending on this thread */  
  19.   sleep(2);  
  20.   while (1) {  
  21.     printf("Secondary thread is now looping/n");  
  22.     ++i;  
  23.     sleep(1);  
  24.     /* pthread_testcancel() has no effect until cancelability is enabled.*/  
  25.     /* At that time, a call to pthread_testcancel() should result in the */  
  26.     /* pending cancel being acted upon                                   */  
  27.     pthread_testcancel();  
  28.     if (i == 5) {  
  29.       printf("Cancel state set to ENABLE/n");  
  30.       rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldState);  
  31.       checkResults("pthread_setcancelstate(2)/n", rc);  
  32.       /* Now, cancellation points will allow pending cancels 
  33.          to get through to this thread */  
  34.     }  
  35.   } /* infinite */  
  36.   pthread_cleanup_pop(0);  
  37.   return NULL;  
  38. }  
  39.   
  40. int main(int argc, char **argv)  
  41. {  
  42.   pthread_t             thread;  
  43.   int                   rc=0;  
  44.   void                 *status=NULL;  
  45.   
  46.   printf("Enter Testcase - %s/n", argv[0]);  
  47.   
  48.   /* Create a thread using default attributes */  
  49.   printf("Create thread using the NULL attributes/n");  
  50.   rc = pthread_create(&thread, NULL, threadfunc, NULL);  
  51.   checkResults("pthread_create(NULL)/n", rc);  
  52.   
  53.   sleep(1);  
  54.   printf("Cancel the thread/n");  
  55.   rc = pthread_cancel(thread);  
  56.   checkResults("pthread_cancel()/n", rc);  
  57.   
  58.   rc = pthread_join(thread, &status);  
  59.   if (status != PTHREAD_CANCELED) {  
  60.     printf("Thread returned unexpected result!/n");  
  61.     exit(1);  
  62.   }  
  63.   printf("Main completed/n");  
  64.   return 0;  
  65. }  
  66.   
  67. Output:  
  68.   
  69. Enter Testcase - QP0WTEST/TPTESTC0  
  70. Create thread using the NULL attributes  
  71. Entered secondary thread  
  72. Cancel the thread  
  73. Secondary thread is now looping  
  74. Secondary thread is now looping  
  75. Secondary thread is now looping  
  76. Secondary thread is now looping  
  77. Secondary thread is now looping  
  78. Cancel state set to ENABLE  
  79. Secondary thread is now looping  
  80. Inside cancellation cleanup handler  
  81. Main completed  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多