pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明
2015-06-14 19:37
387 查看
示例1:
结果:
示例2:
执行结果:
#include <stdio.h> #include <pthread.h> void* clean(void* arg) { printf("cleanup:%s\n", (char*)arg); return (void*)0; } void* thrd_fn1(void* arg) { printf("thrd_fn1 start...\n"); pthread_cleanup_push((void*)clean, "thread1 first handler"); pthread_cleanup_push((void*)clean, "thread1 second handle"); printf("thread1 push complete\n"); if (arg) { return ((void*)1); } pthread_cleanup_pop(1);//当push和pop之间的代码有return时,即使pop的参数为1,也不执行push中的清除函数 pthread_cleanup_pop(1); return (void*)1; } void* thrd_fn2(void* arg) { printf("thrd_fn2 start...\n"); pthread_cleanup_push((void*)clean, "thread2 first handler"); pthread_cleanup_push((void*)clean, "thread2 second handle"); printf("thread2 push complete\n"); if (arg) { pthread_exit((void*)2); } pthread_cleanup_pop(0); pthread_cleanup_pop(0); return (void*)1; } int main(int argc, char** argv) { int nRes = -1; void* pRtVal = 0; pthread_t ptid1,ptid2; nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*)1); if (nRes != 0) { printf("pthread1 creare failed\n"); return -1; } nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*)1); if (nRes != 0) { printf("pthread2 create failed\n"); return -1; } nRes = pthread_join(ptid1, &pRtVal); if (nRes != 0) { printf("pthread_join 1 failed\n"); return -1; } printf("pthread1 exit, code is %d\n", (int)pRtVal); nRes = pthread_join(ptid2, &pRtVal); if (nRes != 0) { printf("pthread_join 1 failed\n"); return -1; } printf("pthread2 exit, code is %d\n", (int)pRtVal); printf("test over!!!\n"); return 0; }
结果:
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:
#include <stdio.h> #include <pthread.h> void* clean(void* arg) { printf("cleanup:%s\n", (char*)arg); return (void*)0; } void* thrd_fn1(void* arg) { printf("thrd_fn1 start...\n"); pthread_cleanup_push((void*)clean, "thread1 first handler pthread_cleanup_push((void*)clean, "thread1 second handle printf("thread1 push complete\n"); pthread_cleanup_pop(1); pthread_cleanup_pop(1); return (void*)1; } void* thrd_fn2(void* arg) { printf("thrd_fn2 start...\n"); pthread_cleanup_push((void*)clean, "thread2 first handler pthread_cleanup_push((void*)clean, "thread2 second handle printf("thread2 push complete\n"); if (arg) { pthread_exit((void*)2); } pthread_cleanup_pop(0); pthread_cleanup_pop(0); return (void*)1; } int main(int argc, char** argv) { int nRes = -1; void* pRtVal = 0; pthread_t ptid1,ptid2; nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*)1); if (nRes != 0) { printf("pthread1 creare failed\n"); return -1; } nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*)1); if (nRes != 0) { printf("pthread2 create failed\n"); return -1; } nRes = pthread_join(ptid1, &pRtVal); if (nRes != 0) { printf("pthread_join 1 failed\n"); return -1; } printf("pthread1 exit, code is %d\n", (int)pRtVal); nRes = pthread_join(ptid2, &pRtVal); if (nRes != 0) { printf("pthread_join 1 failed\n"); return -1; } printf("pthread2 exit, code is %d\n", (int)pRtVal); printf("test over!!!\n"); return 0; }
执行结果:
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!!!
相关文章推荐
- 万剑归宗—架构设计中的抽象思维与具象思维
- 万剑归宗—架构设计中的抽象思维与具象思维
- Linux命令
- opencv和vs 编译生成的程序如何运行在没有安装的电脑上
- 使用 ContentProviderOperation 来提升性能
- 剖析Docker文件系统:Aufs与Devicemapper
- Linux 封闭端口和安全
- linux安全配置记录
- linux命令记录
- centos 5.5 部署apache configure: error: APR not found. Please read the docume
- CentOS防火墙配置
- Linux搭建基于FTP的YUM服务器
- linux 无密码登录
- hadoop 异常记录 ERROR: org.apache.hadoop.hbase.MasterNotRunningException: Retried 7 times
- Linux企业运维高效技巧心得及分享
- 关于VS2015支持编译Linux程序的问题
- Installing Hadoop on Mac OSX Yosemite Tutorial Part 1.
- Linux内核与驱动开发学习总结:PCI中线初始化(十一)
- shell删除指定日期之前修改过的文件
- Centos7安装给firefox 安装adobe flash