标签:
示例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!!!
pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明
标签:
原文地址:http://www.cnblogs.com/black-mamba/p/4575569.html