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 ...
随机推荐
- 【最短路】【spfa】【最小割】【Dinic】bzoj1266 [AHOI2006]上学路线route
原问题等价于断掉一些边,让原来所有的最短路全都无法联通S和T. 先求最短路,然后把在最短路上的边(dis[u[i]]+w[i]==dis[v[i]])加入新图里,跑最小割.显然. 注意是无向图. #i ...
- 【匈牙利算法模板】BZOJ1191-超级英雄
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- a + b ——C语言初学者百题大战之四
#include <stdio.h> int main() { int a,b; scanf("%d %d",&a,&b); printf(" ...
- JAVA Socket 连接时长
其实关于这个问题可能用到的人不会很多,不过我在这里还是说说. 正常很多人写socket通信时,都会直接通过new socket(IP,PORT)直接去链接服务器.其实这种做法也没有错误,但是若当服务器 ...
- Shader and Program编程基本概念 - 转
原地址:http://blog.csdn.net/myarrow/article/details/7737313 一.本文关注的问题: • Shader and program 对象介绍• 创建并编译 ...
- Dell服务器Raid卡电池策略调整
DELL服务器的Riad卡都有可充电池的特性,这块可充电电池,在不使用时,也会有微弱的放电现象,当它的电量放电到低到一定程度时,Raid卡控制器就会对电池进行一次“放电”,将剩余的电量放掉,然后再进行 ...
- javascript快速入门18--样式
修改元素外观方式 修改元素外观主要有下面3种方法:修改ID,修改className,修改元素的style属性 修改ID?会造成多么混乱的结果可想而知! 修改className确实是非常好的方法,我们甚 ...
- HBase设计与开发
HBase设计与开发 @(HBase) 适合HBase应用的场景 成熟的数据分析主题,查询模式已经确定且不会轻易改变. 传统数据库无法承受负载. 简单的查询模式. 基本概念 行健:是hbase表自带的 ...
- EasyUI-解决EasyUI 加载两次url的问题
1.传统方式 $(function () { var url = "../Source/Query/jhDataQry.ashx?action=query"; $(dg).data ...
- Java实现图片裁剪预览功能
在项目中.我们须要做些类似头像上传,图片裁剪的功能,ok看以下文章! 须要插件:jQuery Jcrop 后端代码: package org.csg.upload; import java.awt.R ...