#include
void clean_up(void* arg)
{
printf("\nin clean_up function\n");
printf("arg=%s\n",(char*)arg);
return;
}
void * pthread1(void *arg)
{
printf("pthread begin\n");
pthread_cleanup_push(clean_up,"first first first");
pthread_cleanup_push(clean_up,"second second second");
pthread_cleanup_pop(1);
pthread_cleanup_pop(0);
printf("in the end pthread1\n");
pthread_exit((void *)200);
}
int main(void)
{
int err;
pthread_t tid1;
void * ret;
err=pthread_create(&tid1,NULL,pthread1,NULL);
if(err!=0){
printf("pthread_create error\n");
return -1;
}
err=pthread_join(tid1,&ret);
if(err!=0){
printf("pthread_joid error\n");
return -1;
}
printf("pthread_join return is %d\n",(int)ret);
return 0;
}
++++++++++++++++++++++++++++++++++++
fan@fan:~/arm$ gcc -o app pthread_pop.c -lpthread
fan@fan:~/arm$ ./app
pthread begin
in clean_up function
arg=second second second
in the end pthread1 //其实还是在没有退出子线程的时候调用函数的。
pthread_join return is 200
fan@fan:~/arm$
+++++++++++++++++++++++++++
pop和push的参数都设置为1的情况
fan@fan:~/arm$ gcc -o app pthread_pop.c -lpthread
fan@fan:~/arm$ ./app
pthread begin
in clean_up function
arg=second second second
in clean_up function
arg=first first first
in the end pthread1
pthread_join return is 200