示例1:

 #include <stdio.h>
#include <pthread.h> void* clean(void* arg)
{
printf("cleanup:%s\n", (char*)arg);
return (void*);
} 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*));
} pthread_cleanup_pop(1);//当push和pop之间的代码有return时,即使pop的参数为1,也不执行push中的清除函数
pthread_cleanup_pop();
return (void*);
} 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*));
} pthread_cleanup_pop();
pthread_cleanup_pop();
return (void*);
} int main(int argc, char** argv)
{
int nRes = -;
void* pRtVal = ;
pthread_t ptid1,ptid2; nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*));
if (nRes != )
{
printf("pthread1 creare failed\n");
return -;
} nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*));
if (nRes != )
{
printf("pthread2 create failed\n");
return -;
} nRes = pthread_join(ptid1, &pRtVal);
if (nRes != )
{
printf("pthread_join 1 failed\n");
return -;
}
printf("pthread1 exit, code is %d\n", (int)pRtVal); nRes = pthread_join(ptid2, &pRtVal);
if (nRes != )
{
printf("pthread_join 1 failed\n");
return -;
}
printf("pthread2 exit, code is %d\n", (int)pRtVal); printf("test over!!!\n"); return ;
}

结果:

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

示例2:

 #include <stdio.h>
#include <pthread.h> void* clean(void* arg)
{
printf("cleanup:%s\n", (char*)arg);
return (void*);
} 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();
pthread_cleanup_pop();
return (void*);
} 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*));
} pthread_cleanup_pop();
pthread_cleanup_pop();
return (void*);
} int main(int argc, char** argv)
{
int nRes = -;
void* pRtVal = ;
pthread_t ptid1,ptid2; nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*));
if (nRes != )
{
printf("pthread1 creare failed\n");
return -;
} nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*));
if (nRes != )
{
printf("pthread2 create failed\n");
return -;
} nRes = pthread_join(ptid1, &pRtVal);
if (nRes != )
{
printf("pthread_join 1 failed\n");
return -;
}
printf("pthread1 exit, code is %d\n", (int)pRtVal); nRes = pthread_join(ptid2, &pRtVal);
if (nRes != )
{
printf("pthread_join 1 failed\n");
return -;
}
printf("pthread2 exit, code is %d\n", (int)pRtVal); printf("test over!!!\n"); return ;
}

执行结果:

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
pthread2 exit, code is
test over!!!

最新文章

  1. Android -- 案例学习积累
  2. FC400的PM2.5过滤效率测试。。
  3. Docker镜像文件存储结构
  4. Nutch源码阅读进程5---updatedb
  5. Nginx系列5之让Nginx支持HTTP1.1
  6. [LeetCode] Min Stack
  7. JMeter学习-008-JMeter 后置处理器实例之 - 正则表达式提取器(一)概述及简单实例
  8. centos6.5用户添加到sudoers中
  9. 高斯混合模型参数估计的EM算法
  10. 【优先队列】HDU 1873——看病找医生
  11. 深入分析 Java 中的中文编码问题(转)
  12. [VBA]发布一个计算桩号之差的Excel自定义函数(VBA)
  13. hex和bin文件格式的区别
  14. android 4.0 中出错 java.lang.UnsupportedOperationException
  15. 2014ACM/ICPC亚洲区域赛牡丹江站汇总
  16. Windows 8 卡在正在检查更新
  17. 【python】列表
  18. JavaWeb学习笔记二 Http协议和Tomcat服务器
  19. 【ARC072E】Alice in linear land DP
  20. 「SPOJ6340」「BZOJ1939」ZUMA - ZUMA【记忆化搜索】

热门文章

  1. 【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截
  2. MetaWeblog是什么
  3. MathType输入矩阵或者向量的注意事项
  4. [Git] 关于refs/for/ 和refs/heads/
  5. SpringMVC学习记录(七)--拦截器的使用
  6. Render Texture coordinates
  7. Unity GPU Query OpenGLES 3.0
  8. 代码这样写更优雅(Python 版)(转载)
  9. Android画图系列(二)——自己定义View绘制基本图形
  10. [转载]Oracle Merge的使用