日期:2014-05-16 浏览次数:20764 次
#include <stdio.h>
#include <pthread.h>
void *Clean(void *pArg)
{
printf("cleanup: %s \n" ,((char *)pArg));
return (void *)0;
}
void *Thread1(void *pArg)
{
printf("thread 1 start \n");
pthread_cleanup_push((void *)Clean, "thread 1 first handler");
pthread_cleanup_push((void *)Clean, "thread 1 second handler");
printf("thread 1 push complete. \n");
if(pArg)
{
/* 这里使用的是return 返回 所以不会调用 pthread_cleanup_push 指定的Clean*/
return (void *)1;
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void *)1;
}
void *Thread2(void *pArg)
{
printf("thread 2 start \n");
pthread_cleanup_push((void *)Clean, "thread 2 first handler");
pthread_cleanup_push((void *)Clean, "thread 2 second handler");
printf("thread 2 push complete. \n");
if(pArg)
{
/*
void pthread_exit(void *rval_ptr)
功能:终止调用线程
rval_ptr:线程退出返回值的指针*/
pthread_exit((void *)2);
}
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return (void *)2;
}
int main(void)
{
pthread_t tThreadID1, tThreadID2;
int iRst;
void *tRst;
printf("this is main thread\n");
iRst = pthread_create(&tThreadID1, NULL, (void *)Thread1, (void *)1);
if(iRst!=0)
{
printf("create thread1 error .... \n");
re