示例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清除函数是否执行的说明的更多相关文章

  1. Linux 下子线程的 pthread_cleanup_push() 和 pthread_cleanup_pop() 研究

    线程退出前可能有一些清理工作,但是这部分代码又不会放到线程主体部分,就需要挂接一个或者几个线程“清洁工”来做这部分事情.需要这对兄弟: #include<pthread.h> void p ...

  2. 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 ...

  3. pthread_cleanup_push与pthread_cleanup_pop的理解

    一.为什么会有pthread_cleanup_push与pthread_cleanup_pop: 一般来说,Posix的线程终止有两种情况:正常终止和非正常终止.线程主动调用pthread_exit( ...

  4. android view 中各函数的执行顺数

    这个就好像是 activity 的生命周期一样,如果我们要使用自定义的 view,那么就很有必要了解一下 view 的那些能够被重写的函数的执行顺序.废话不多讲,以常用的5个函数为例子,见下文: pa ...

  5. $.getJSON('url',function(data){}) 中回调函数不执行

    $.getJSON('url',function(data){}) 中回调函数不执行 url 中的 json 格式不正确 ,浏览器返回并没有报错 {'湖北':[114.11438,30.849429] ...

  6. 全局对象的构造函数会在main 函数之前执行

    #include <iostream> using namespace std; class A { public: A() { cout << "Generator ...

  7. unity3D技术之事件函数的执行顺序[转]

    unity3D技术之事件函数的执行顺序 转自http://www.yxkfw.com/?p=13703   在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: ...

  8. Unity3D中自带事件函数的执行顺序(转)

    原文:http://www.jianshu.com/p/1d93ece664e2 在Unity3D脚本中,有几个Unity3D自带的事件函数按照预定的顺序执行作为脚本执行.其执行顺序如下: 编辑器(E ...

  9. Scala 中 构造函数,重载函数的执行顺序

    在调试scala在线开发教程(http://www.imobilebbs.com/wordpress/archives/4911)的过程中看到了以下代码,但是这段代码无论怎么调试都无法成功. abst ...

随机推荐

  1. 【动态规划】【最短路】【spfa】bzoj1207 [HNOI2004]打鼹鼠

    <法一>若打了一只鼹鼠后,还能打另一只,我们可以在它们之间连权值为1的边.于是答案就是 以m为终点的最长路长度+1.建反图,就是单源最长路. MLE TLE 一时爽. #include&l ...

  2. 【分块】bzoj2724 [Violet 6]蒲公英

    分块,离散化,预处理出: ①前i块中x出现的次数(差分): ②第i块到第j块中的众数是谁,出现了多少次. 询问的时候,对于整块的部分直接获得答案:对于零散的部分,暴力统计每个数出现的次数,加上差分的结 ...

  3. 【2-SAT(两次DFS版)】BZOJ1823-[JSOI2010]满汉全席

    [题目大意] 有n个材料,m个评委.每种材料可以被用来做满族菜或汉族菜,m个评委有两种可以让他满意的猜中.问是否可以满足所有评委要求? [思路] 每天只能做三道题,我已经是一个废人了……(葛优躺.jp ...

  4. 数据挖掘经典算法——K-means算法

    算法描述 K-means算法是一种被广泛使用的基于划分的聚类算法,目的是将n个对象会分成k个簇.算法的具体描述如下: 随机选取k个对象作为簇中心: Do 计算所有对象到这k个簇中心的距离,将距离最近的 ...

  5. Ubuntu -- 安装和部署php5.6 nginx php5.6-fpm

      1.首先输入用户名和密码进行登录 2.升级更新软件包 sudo apt-get update sudo apt-get upgrade 判断都填y 3.安装nginx sudo apt-get i ...

  6. ActiveX控件在项目中的应用

  7. canvas如何兼容IE8

    大家都知道canvas是个非常好玩的东西,但是IE9以下的浏览器不支持,有时候业务需求必须用到canvas,且又要求兼容IE8浏览器,那怎么办呢? 1.添加对html5的支持:<!--[if I ...

  8. javascript中常见的函数封装 :判断是否是手机,判断是否是微信,获取url地址?后面的具体参数值,毫秒格式化时间,手机端px、rem尺寸转换等

    // 判断是否是手机function plat_is_mobile(){ var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad ...

  9. python获取系统信息,

    1 操作系统系统综合信息cur_sysinfo=platform.uname()浏览器信息cur_browserinfo=self.driver.capabilities['version']浏览器新 ...

  10. npm依赖管理:冗余,依赖树

    npm的依赖树查询:原理都是查询文件夹node_modules的结构.比如mac的node_modules位置在/usr/local/lib下.具体项目的node_modules位置位于项目根目录下. ...