pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明

示例1:

 1 #include <stdio.h>
 2 #include <pthread.h>
 3
 4 void* clean(void* arg)
 5 {
 6     printf("cleanup:%s\n", (char*)arg);
 7     return (void*)0;
 8 }
 9
10 void* thrd_fn1(void* arg)
11 {
12     printf("thrd_fn1 start...\n");
13     pthread_cleanup_push((void*)clean, "thread1 first handler");
14     pthread_cleanup_push((void*)clean, "thread1 second handle");
15     printf("thread1 push complete\n");
16     if (arg)
17     {
18         return ((void*)1);
19     }
20
21     pthread_cleanup_pop(1);//当push和pop之间的代码有return时,即使pop的参数为1,也不执行push中的清除函数
22     pthread_cleanup_pop(1);
23     return (void*)1;
24 }
25
26 void* thrd_fn2(void* arg)
27 {
28     printf("thrd_fn2 start...\n");
29     pthread_cleanup_push((void*)clean, "thread2 first handler");
30     pthread_cleanup_push((void*)clean, "thread2 second handle");
31     printf("thread2 push complete\n");
32     if (arg)
33     {
34         pthread_exit((void*)2);
35     }
36
37     pthread_cleanup_pop(0);
38     pthread_cleanup_pop(0);
39     return (void*)1;
40 }
41
42 int main(int argc, char** argv)
43 {
44     int nRes = -1;
45     void* pRtVal = 0;
46     pthread_t ptid1,ptid2;
47
48     nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*)1);
49     if (nRes != 0)
50     {
51         printf("pthread1 creare failed\n");
52         return -1;
53     }
54
55     nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*)1);
56     if (nRes != 0)
57     {
58         printf("pthread2 create failed\n");
59         return -1;
60     }
61
62     nRes = pthread_join(ptid1, &pRtVal);
63     if (nRes != 0)
64     {
65         printf("pthread_join 1 failed\n");
66         return -1;
67     }
68     printf("pthread1 exit, code is %d\n", (int)pRtVal);
69
70     nRes = pthread_join(ptid2, &pRtVal);
71     if (nRes != 0)
72     {
73         printf("pthread_join 1 failed\n");
74         return -1;
75     }
76     printf("pthread2 exit, code is %d\n", (int)pRtVal);
77
78     printf("test over!!!\n");
79
80     return 0;
81 }

结果:

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

示例2:

 1 #include <stdio.h>
 2 #include <pthread.h>
 3
 4 void* clean(void* arg)
 5 {
 6     printf("cleanup:%s\n", (char*)arg);
 7     return (void*)0;
 8 }
 9
10 void* thrd_fn1(void* arg)
11 {
12     printf("thrd_fn1 start...\n");
13     pthread_cleanup_push((void*)clean, "thread1 first handler
14     pthread_cleanup_push((void*)clean, "thread1 second handle
15     printf("thread1 push complete\n");
16
17     pthread_cleanup_pop(1);
18     pthread_cleanup_pop(1);
19     return (void*)1;
20 }
21
22 void* thrd_fn2(void* arg)
23 {
24     printf("thrd_fn2 start...\n");
25     pthread_cleanup_push((void*)clean, "thread2 first handler
26     pthread_cleanup_push((void*)clean, "thread2 second handle
27     printf("thread2 push complete\n");
28     if (arg)
29     {
30         pthread_exit((void*)2);
31     }
32
33     pthread_cleanup_pop(0);
34     pthread_cleanup_pop(0);
35     return (void*)1;
36 }
37
38 int main(int argc, char** argv)
39 {
40     int nRes = -1;
41     void* pRtVal = 0;
42     pthread_t ptid1,ptid2;
43
44     nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*)1);
45     if (nRes != 0)
46     {
47         printf("pthread1 creare failed\n");
48         return -1;
49     }
50
51     nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*)1);
52     if (nRes != 0)
53     {
54         printf("pthread2 create failed\n");
55         return -1;
56     }
57
58     nRes = pthread_join(ptid1, &pRtVal);
59     if (nRes != 0)
60     {
61         printf("pthread_join 1 failed\n");
62         return -1;
63     }
64     printf("pthread1 exit, code is %d\n", (int)pRtVal);
65
66     nRes = pthread_join(ptid2, &pRtVal);
67     if (nRes != 0)
68     {
69         printf("pthread_join 1 failed\n");
70         return -1;
71     }
72     printf("pthread2 exit, code is %d\n", (int)pRtVal);
73
74     printf("test over!!!\n");
75
76     return 0;
77 }

执行结果:

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 1
pthread2 exit, code is 2
test over!!!
时间: 2024-08-01 23:54:03

pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明的相关文章

【酷熊科技】工作积累 ----------- Unity3d中的Awake()、OnEnable()、Start()等默认函数的执行顺序和生命周期

Awake()在MonoBehavior创建后就立刻调用,在脚本实例的整个生命周期中,Awake函数仅执行一次:如果游戏对象(即gameObject)的初始状态为关闭状态,那么运行程序,Awake函数不会执行:如果游戏对象的初始状态为开启状态,那么Awake函数会执行:值得注意的一点是,Awake函数的执行与否与脚本实例的状态(启用或禁用)并没有关系,而是与脚本实例所绑定的游戏对象的开关状态有关.如果重新加载场景,那么场景内Awake函数的执行情况重新遵循上述两点. Start()将在MonoB

函数自执行的错误与正确示范

[1]函数表达式 [1.1]引用执行 var foo = function(){ alert(1); }; foo();//弹出1 [1.2]传参执行 var foo = function(i){ alert(i);//弹出1 }(1); [2]三种函数自执行的错误写法 [2.1]出错提示函数声明需要一个名字 function(){ alert(1); }(); [2.2]出错提示少右括号 function abc(){ alert(1); }(); [2.3]无错,但foo函数未执行 func

学习js函数--自执行函数

我在写代码时候经常会在tpl的<script>里写类似的代码: $(function(){ alert("我好饿"); }); 刚开始的时候只知道写了它不需要调用,直接执行,就这样依葫芦画瓢,我写了很多代码.说道这,还要说说这货的加载顺序,如果把代码直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了.如果在这代码里用到了未加载的dom或者调用了未加载的方法,是会报错的.言归正传,这个函数其实就是自执行函数,很多人会比较专业地称为"立即

多玩YY语音的面试题:C++中如何在main()函数之前执行操作?

第一反应main()函数是所有函数执行的开始.但是问题是main()函数执行之前如何执行呢? 联想到MFC里面的 C**App类的theApp对象,其执行顺序就在main函数之前.道理相通,顺理推下,能够想到:如果在main函数之前声明一个类的全局的对象.那么其执行顺序,根据全局对象的生存期和作用域,肯定先于main函数. 示例如下: class simpleClass { public: simpleClass( ) { cout << "simpleClass construct

事件函数的执行顺序

 事件函数的执行顺序 在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: Editor Reset: Reset调用来初始化脚本的属性,当它第一次附加到该对象,并且使用Reset命令时. 第一次Scene Load scene启动 (一次为每个场景中的对象) 时,会调用这些函数. Awake:此函数始终是开始任何职能之前调用,并且也是在一个预置实例化之后.(如果一个游戏对象处于非活动状态,Awake不会被调用当被激活时 或者一个附加到游戏对象的任何脚本

$.getJSON() 回调函数没有执行的原因

$.getJSON() 方法使用 AJAX 的 HTTP GET 请求获取 JSON 数据. 语法 $.getJSON(url,data,success(data,status,xhr)) url必填规定请求发送到那个url: data可选规定发送到服务器的数据: success可选data包含服务器返回的数据, status包含请求的状态,("success"."notmodified"."error"."timeout".

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

#include <iostream> using namespace std; class A { public: A() { cout << "Generator A" << endl; } } a = A(); int main() { cout << "Hello World" << endl; } 全局对象的构造会在main函数之前执行.

JavaScript 函数的执行过程

每一个JavaScript函数都是Function对象的一个实例, 它有一个仅供JavaScript引擎存取的内部属性[[Scope]]. 这个[[Scope]]存储着一个作用域的集合, 这个集合就叫”作用域链”, 集合中存储着”可变对象”VO或”活动对象”AO(AO比VO多this和arguments属性). 当函数被创建后, 其父级作用域的作用域链中的所有可变对象会被加入到它的[[scope]]中(如果父作用域是全局, 那么当前函数的作用域链中就只会加入一个全局对象). 当函数被执行时, 函

jQuery源码,匿名函数自执行

jQuery框架的首尾是这样写的()(), 1 (function(window){//这个window是个入参,随便起个名字都行 2 //这里面全都是js代码 3 })(window)//这个括号里的window不能变 实际上上面代码中后面的那个window是传入的js中的window对象,前面是也是变量名,只不过也叫window罢了.这种()()方式叫做匿名函数自执行. 上面的代码完全可以写成下面这样: 1 (function(w){ 2 //这里面全都是js代码 3 w.$=w.jQuer