您的位置:首页 > 运维架构

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

2015-06-14 19:37 387 查看
示例1:

#include <stdio.h>
#include <pthread.h>

void* clean(void* arg)
{
printf("cleanup:%s\n", (char*)arg);
return (void*)0;
}

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*)1);
}

pthread_cleanup_pop(1);//当push和pop之间的代码有return时,即使pop的参数为1,也不执行push中的清除函数
pthread_cleanup_pop(1);
return (void*)1;
}

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*)2);
}

pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void*)1;
}

int main(int argc, char** argv)
{
int nRes = -1;
void* pRtVal = 0;
pthread_t ptid1,ptid2;

nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*)1);
if (nRes != 0)
{
printf("pthread1 creare failed\n");
return -1;
}

nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*)1);
if (nRes != 0)
{
printf("pthread2 create failed\n");
return -1;
}

nRes = pthread_join(ptid1, &pRtVal);
if (nRes != 0)
{
printf("pthread_join 1 failed\n");
return -1;
}
printf("pthread1 exit, code is %d\n", (int)pRtVal);

nRes = pthread_join(ptid2, &pRtVal);
if (nRes != 0)
{
printf("pthread_join 1 failed\n");
return -1;
}
printf("pthread2 exit, code is %d\n", (int)pRtVal);

printf("test over!!!\n");

return 0;
}


结果:

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:

#include <stdio.h>
#include <pthread.h>

void* clean(void* arg)
{
printf("cleanup:%s\n", (char*)arg);
return (void*)0;
}

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(1);
pthread_cleanup_pop(1);
return (void*)1;
}

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*)2);
}

pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void*)1;
}

int main(int argc, char** argv)
{
int nRes = -1;
void* pRtVal = 0;
pthread_t ptid1,ptid2;

nRes = pthread_create(&ptid1, NULL, thrd_fn1, (void*)1);
if (nRes != 0)
{
printf("pthread1 creare failed\n");
return -1;
}

nRes = pthread_create(&ptid2, NULL, thrd_fn2, (void*)1);
if (nRes != 0)
{
printf("pthread2 create failed\n");
return -1;
}

nRes = pthread_join(ptid1, &pRtVal);
if (nRes != 0)
{
printf("pthread_join 1 failed\n");
return -1;
}
printf("pthread1 exit, code is %d\n", (int)pRtVal);

nRes = pthread_join(ptid2, &pRtVal);
if (nRes != 0)
{
printf("pthread_join 1 failed\n");
return -1;
}
printf("pthread2 exit, code is %d\n", (int)pRtVal);

printf("test over!!!\n");

return 0;
}


执行结果:

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!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: