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 ...
随机推荐
- Codeforces 786C Till I Collapse(树状数组+扫描线+倍增)
[题目链接] http://codeforces.com/contest/786/problem/C [题目大意] 给出一个数列,问对于不同的k,将区间划分为几个, 每个区间出现不同元素个数不超过k时 ...
- 【并查集+离散化】BZOJ4195- [Noi2015]程序自动分析
[题目大意] 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的 ...
- 【OpenJudge8464】【序列DP】股票买卖
股票买卖 总时间限制: 1000ms 内存限制: 65536kB [描述] 最近越来越多的人都投身股市,阿福也有点心动了.谨记着“股市有风险,入市需谨慎”,阿福决定先来研究一下简化版的股票买卖问题. ...
- 获取OS X中App Store更新后的安装包(如XCode)
如果宿舍有好几个人需要更新一些大的软件,如XCode,会占用很大的带宽. 为了节省带宽,我们可以在1台电脑上更新完后,获取存放在系统暂存区的更新的安装包,然后通过局域网或Airdrop的方式轻松分 ...
- redis系列文章
http://blog.csdn.net/liubenlong007/article/details/53690103
- VS2017序列号|Visual Studio 2017 激活码 序列号
企业版:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF 专业版:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH
- 专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路(HipHop,HHVM)
专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路 http://www.infoq.com/cn/articles/interview-alibaba-zhaohaiping
- 会话对应的线程id
http://blog.csdn.net/sqlserverdiscovery/article/details/7968117
- Linux下交叉编译gdb和gdbserver
平台:tq2440 GCC: gcc version 4.3.3 (Sourcery G++ Lite 2009q1-176) 这里过程中参考了下面两篇博文: http://blog.csdn.ne ...
- mysql性能检查脚本-部分
#!/bin/sh #ocpyang@126.com export black='\033[0m' export boldblack='\033[1;0m' export red='\033[31m' ...