zoukankan      html  css  js  c++  java
  • pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明

    示例1:

     1 #include <stdio.h>
     2 #include <pthread.h>
     3 
     4 void* clean(void* arg)
     5 {
     6     printf("cleanup:%s
    ", (char*)arg);
     7     return (void*)0;
     8 }
     9 
    10 void* thrd_fn1(void* arg)
    11 {
    12     printf("thrd_fn1 start...
    ");
    13     pthread_cleanup_push((void*)clean, "thread1 first handler");
    14     pthread_cleanup_push((void*)clean, "thread1 second handle");
    15     printf("thread1 push complete
    ");
    16     if (arg)
    17     {
    18         return ((void*)1);
    19     }
    20 
    21     pthread_cleanup_pop(1);//当push和pop之间的代码有return时,即使pop的参数为1,也不执行push中的清除函数
    22     pthread_cleanup_pop(1);
    23     return (void*)1;
    24 }
    25 
    26 void* thrd_fn2(void* arg)
    27 {
    28     printf("thrd_fn2 start...
    ");
    29     pthread_cleanup_push((void*)clean, "thread2 first handler");
    30     pthread_cleanup_push((void*)clean, "thread2 second handle");
    31     printf("thread2 push complete
    ");
    32     if (arg)
    33     {
    34         pthread_exit((void*)2);
    35     }
    36 
    37     pthread_cleanup_pop(0);
    38     pthread_cleanup_pop(0);
    39     return (void*)1;
    40 }
    41 
    42 int main(int argc, char** argv)
    43 {
    44     int nRes = -1;
    45     void* pRtVal = 0;
    46     pthread_t ptid1,ptid2;
    47 
    48     nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*)1);
    49     if (nRes != 0)
    50     {
    51         printf("pthread1 creare failed
    ");
    52         return -1;
    53     }
    54 
    55     nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*)1);
    56     if (nRes != 0)
    57     {
    58         printf("pthread2 create failed
    ");
    59         return -1;
    60     }
    61 
    62     nRes = pthread_join(ptid1, &pRtVal);
    63     if (nRes != 0)
    64     {
    65         printf("pthread_join 1 failed
    ");
    66         return -1;
    67     }
    68     printf("pthread1 exit, code is %d
    ", (int)pRtVal);
    69 
    70     nRes = pthread_join(ptid2, &pRtVal);
    71     if (nRes != 0)
    72     {
    73         printf("pthread_join 1 failed
    ");
    74         return -1;
    75     }
    76     printf("pthread2 exit, code is %d
    ", (int)pRtVal);
    77 
    78     printf("test over!!!
    ");
    79 
    80     return 0;
    81 }

    结果:

    thrd_fn2 start...
    thread2 push complete
    cleanup:thread2 second handler
    cleanup:thread2 first handler
    thrd_fn1 start...
    thread1 push complete
    pthread1 exit, code is 1
    pthread2 exit, code is 2
    test over!!!

    示例2:

     1 #include <stdio.h>
     2 #include <pthread.h>
     3 
     4 void* clean(void* arg)
     5 {
     6     printf("cleanup:%s
    ", (char*)arg);
     7     return (void*)0;
     8 }
     9 
    10 void* thrd_fn1(void* arg)
    11 {
    12     printf("thrd_fn1 start...
    ");
    13     pthread_cleanup_push((void*)clean, "thread1 first handler
    14     pthread_cleanup_push((void*)clean, "thread1 second handle
    15     printf("thread1 push complete
    ");
    16 
    17     pthread_cleanup_pop(1);
    18     pthread_cleanup_pop(1);
    19     return (void*)1;
    20 }
    21 
    22 void* thrd_fn2(void* arg)
    23 {
    24     printf("thrd_fn2 start...
    ");
    25     pthread_cleanup_push((void*)clean, "thread2 first handler
    26     pthread_cleanup_push((void*)clean, "thread2 second handle
    27     printf("thread2 push complete
    ");
    28     if (arg)
    29     {
    30         pthread_exit((void*)2);
    31     }
    32 
    33     pthread_cleanup_pop(0);
    34     pthread_cleanup_pop(0);
    35     return (void*)1;
    36 }
    37 
    38 int main(int argc, char** argv)
    39 {
    40     int nRes = -1;
    41     void* pRtVal = 0;
    42     pthread_t ptid1,ptid2;
    43 
    44     nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*)1);
    45     if (nRes != 0)
    46     {
    47         printf("pthread1 creare failed
    ");
    48         return -1;
    49     }
    50 
    51     nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*)1);
    52     if (nRes != 0)
    53     {
    54         printf("pthread2 create failed
    ");
    55         return -1;
    56     }
    57 
    58     nRes = pthread_join(ptid1, &pRtVal);
    59     if (nRes != 0)
    60     {
    61         printf("pthread_join 1 failed
    ");
    62         return -1;
    63     }
    64     printf("pthread1 exit, code is %d
    ", (int)pRtVal);
    65 
    66     nRes = pthread_join(ptid2, &pRtVal);
    67     if (nRes != 0)
    68     {
    69         printf("pthread_join 1 failed
    ");
    70         return -1;
    71     }
    72     printf("pthread2 exit, code is %d
    ", (int)pRtVal);
    73 
    74     printf("test over!!!
    ");
    75 
    76     return 0;
    77 }

    执行结果:

    thrd_fn2 start...
    thread2 push complete
    cleanup:thread2 second handler
    cleanup:thread2 first handler
    thrd_fn1 start...
    thread1 push complete
    cleanup:thread1 second handler
    cleanup:thread1 first handler
    pthread1 exit, code is 1
    pthread2 exit, code is 2
    test over!!!
  • 相关阅读:
    Taro、小程序使用定位服务
    Taro项目中设置了设计稿尺寸为375,taro-ui的样式会被放大
    Taro 页面返回携带参数
    Taro + TS 项目取别名配置 alias
    安卓APP 错误:net::ERR_CLEARTEXT_NOT_PERMITTED解决方法
    Springboot定时发送邮件,并附带Excel文件和PDF文件
    通过openssl导入证书到系统证书目录解决安卓7以上系统无法抓包问题
    CentOS 7 安装配置SVN服务器
    解决安装 Docker 慢:使用国内阿里云镜像加速安装
    Redis实战篇(四)基于GEO实现查找附近的人功能
  • 原文地址:https://www.cnblogs.com/black-mamba/p/4575569.html
Copyright © 2011-2022 走看看