pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明
示例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!!!
pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明的更多相关文章
- Linux 下子线程的 pthread_cleanup_push() 和 pthread_cleanup_pop() 研究
		
线程退出前可能有一些清理工作,但是这部分代码又不会放到线程主体部分,就需要挂接一个或者几个线程“清洁工”来做这部分事情.需要这对兄弟: #include<pthread.h> void p ...
 - pthread_cleanup_push与pthread_cleanup_pop与pthread_cancel与pthread_testcancel
		
参考: http://blog.csdn.net/zjc156m/article/details/9021343 http://blog.csdn.net/u010027547/article/det ...
 - pthread_cleanup_push与pthread_cleanup_pop的理解
		
一.为什么会有pthread_cleanup_push与pthread_cleanup_pop: 一般来说,Posix的线程终止有两种情况:正常终止和非正常终止.线程主动调用pthread_exit( ...
 - android view 中各函数的执行顺数
		
这个就好像是 activity 的生命周期一样,如果我们要使用自定义的 view,那么就很有必要了解一下 view 的那些能够被重写的函数的执行顺序.废话不多讲,以常用的5个函数为例子,见下文: pa ...
 - $.getJSON('url',function(data){})  中回调函数不执行
		
$.getJSON('url',function(data){}) 中回调函数不执行 url 中的 json 格式不正确 ,浏览器返回并没有报错 {'湖北':[114.11438,30.849429] ...
 - 全局对象的构造函数会在main 函数之前执行
		
#include <iostream> using namespace std; class A { public: A() { cout << "Generator ...
 - unity3D技术之事件函数的执行顺序[转]
		
unity3D技术之事件函数的执行顺序 转自http://www.yxkfw.com/?p=13703 在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: ...
 - Unity3D中自带事件函数的执行顺序(转)
		
原文:http://www.jianshu.com/p/1d93ece664e2 在Unity3D脚本中,有几个Unity3D自带的事件函数按照预定的顺序执行作为脚本执行.其执行顺序如下: 编辑器(E ...
 - Scala 中 构造函数,重载函数的执行顺序
		
在调试scala在线开发教程(http://www.imobilebbs.com/wordpress/archives/4911)的过程中看到了以下代码,但是这段代码无论怎么调试都无法成功. abst ...
 
随机推荐
- 【莫队算法】【权值分块】bzoj3585 mex
			
orz PoPoQQQ. 本来蒟蒻以为这种离散化以后就对应不起来的题不能权值分块搞的说. ……结果,实际上>n的权值不会对答案作出贡献. #include<cstdio> #incl ...
 - 【最短路】【Heap-Dijkstra】【分层图】bzoj2662 [BeiJing wc2012]冻结
			
裸的分层图最短路. #include<cstdio> #include<cstring> #include<queue> #include<algorithm ...
 - 杂谈PID控制算法——第二篇:调·三个量
			
上面一篇文章讲了一下PID算法中的三个常量大致的在PID算法中起的一个作用,但在实际的使用中,究竟应该如何调节(或者用更加专业的话说是整定)PID控制算法的三个.首先可以将KP,KI,KD三个常量全部 ...
 - MVC Movie App
			
ylbtech- ASP.NET MVC:MVC Movie App 功能描述:MVC Movie App 2,TechnologyAndTheEnvironment(技术与环境) 操作系统: win ...
 - javascript快速入门8--值,类型与类型转换
			
原始值和引用值 在ECMAScript中,变量可以存放两种类型的值,即原始值和引用值. 原始值(primitive value)是存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量 ...
 - 2017.11.15 linux软件安装管理(todo)
			
学习来自:http://www.imooc.com/learn/447 第一章 介绍 第二章 软件包简介 1.源码包 2.二进制包(RPM包或系统默认包) 脚本安装包其实是别人把软件安装的脚本写好了, ...
 - testform
			
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
 - 基于zookeeper+leveldb搭建activemq集群--转载
			
原地址:http://www.open-open.com/lib/view/open1410569018211.html 自从activemq5.9.0开始,activemq的集群实现方式取消了传统的 ...
 - 怎样改动SharePoint管理中心的语言
			
在安装了语言包之后,创建站点集的时候,就能够选择语言了. 可是SharePoint管理中心的语言没有变.这个时候.怎么才干让管理中心也使用新的语言呢? 能够依照下面方法. 首先去https://msd ...
 - 怎样实现广度优先遍历(BFS)
			
BFS过程: 一:訪问顶点V,并标记V为已经訪问 二:顶点V入队列 三:假设队列非空.进行运行,否则算法结束 四:出队列取得对头顶点u,假设顶点未被訪问,就訪问该顶点,并标记该顶点为已经訪问 五:查找 ...