当前位置: 技术问答>linux和unix
pthread_cleanup_push函数与pthread_cleanup_pop函数问题
来源: 互联网 发布时间:2017-01-11
本文导语: #include #include #include void cleanup(void*arg) { printf("NO.%d clean up procduren",*(int*)arg); } void*tfn1(void*arg) { int a=1; printf("the first threadn"); pthread_cleanup_push(cleanup,&a); a=2; pthread_cleanup_push(cleanup,&a); return NULL; pthread_cleanup_pop(0...
#include
#include
#include
void cleanup(void*arg)
{
printf("NO.%d clean up procduren",*(int*)arg);
}
void*tfn1(void*arg)
{
int a=1;
printf("the first threadn");
pthread_cleanup_push(cleanup,&a);
a=2;
pthread_cleanup_push(cleanup,&a);
return NULL;
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return NULL;
}
void *tfn2(void*arg)
{
int a=1;
printf("the second threadn");
pthread_cleanup_push(cleanup,&a);
a=2;
pthread_cleanup_push(cleanup,&a);
pthread_exit(NULL);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return NULL;
}
void*tfn3(void*arg)
{
int a=1;
printf("the third threadn");
pthread_cleanup_push(cleanup,&a);
a=2;
pthread_cleanup_push(cleanup,&a);
pthread_cleanup_pop(1);
printf("ready to sleepn");
sleep(10);
pthread_cleanup_pop(0);
return NULL;
}
int main()
{
pthread_t tid1,tid2,tid3,tid4;
int err;
err=pthread_create(&tid1,NULL,tfn1,NULL);
if(err!=0)
{
printf("can't create thread %sn",strerror(err));
exit(1);
}
err=pthread_join(tid1,NULL);
if(err!=0)
{
printf("can't join thread %sn",strerror(err));
exit(1);
}
err=pthread_create(&tid2,NULL,tfn2,NULL);
if(err!=0)
{
printf("can't create thread %sn",strerror(err));
exit(1);
}
err=pthread_join(tid2,NULL);
if(err!=0)
{
printf("can't join thread %sn",strerror(err));
exit(1);
}
err=pthread_create(&tid3,NULL,tfn3,NULL);
if(err!=0)
{
printf("can't create thread %sn",strerror(err));
exit(1);
}
err=pthread_cancel(tid3);
if(err!=0)
{
printf("can't cancel thread %sn",strerror(err));
exit(1);
}
err=pthread_join(tid3,NULL);
if(err!=0)
{
printf("can't join thread %sn",strerror(err));
exit(1);
}
return 0;
}
这段程序执行结果如下:
the first thread
the second thread
NO.2 clean up procdure
NO.2 clean up procdure
the third thread
NO.2 clean up procdure
ready to sleep
NO.2 clean up procdure
这里我有好几个问题:
1.在这个函数中,为什么有两个NULL,执行到第一个NULL处,线程已经退出,下面的两个函数(pthread_cleanup_pop(0);
)怎么会执行到?,如下:
void*tfn1(void*arg)
{
int a=1;
printf("the first threadn");
pthread_cleanup_push(cleanup,&a);
a=2;
pthread_cleanup_push(cleanup,&a);
return NULL;//
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return NULL;//为什么有两个NULL,执行到第一个NULL处,线程已经退出,下面的两个函数(pthread_cleanup_pop(0);
)怎么会执行到?
}
2.执行结果中,出现:
the second thread
NO.2 clean up procdure
NO.2 clean up procdure
为什么不是:
the second thread
NO.2 clean up procdure
NO.1clean up procdure
这个执行结果似乎更合理。
请高手指教!多谢!
#include
#include
void cleanup(void*arg)
{
printf("NO.%d clean up procduren",*(int*)arg);
}
void*tfn1(void*arg)
{
int a=1;
printf("the first threadn");
pthread_cleanup_push(cleanup,&a);
a=2;
pthread_cleanup_push(cleanup,&a);
return NULL;
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return NULL;
}
void *tfn2(void*arg)
{
int a=1;
printf("the second threadn");
pthread_cleanup_push(cleanup,&a);
a=2;
pthread_cleanup_push(cleanup,&a);
pthread_exit(NULL);
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return NULL;
}
void*tfn3(void*arg)
{
int a=1;
printf("the third threadn");
pthread_cleanup_push(cleanup,&a);
a=2;
pthread_cleanup_push(cleanup,&a);
pthread_cleanup_pop(1);
printf("ready to sleepn");
sleep(10);
pthread_cleanup_pop(0);
return NULL;
}
int main()
{
pthread_t tid1,tid2,tid3,tid4;
int err;
err=pthread_create(&tid1,NULL,tfn1,NULL);
if(err!=0)
{
printf("can't create thread %sn",strerror(err));
exit(1);
}
err=pthread_join(tid1,NULL);
if(err!=0)
{
printf("can't join thread %sn",strerror(err));
exit(1);
}
err=pthread_create(&tid2,NULL,tfn2,NULL);
if(err!=0)
{
printf("can't create thread %sn",strerror(err));
exit(1);
}
err=pthread_join(tid2,NULL);
if(err!=0)
{
printf("can't join thread %sn",strerror(err));
exit(1);
}
err=pthread_create(&tid3,NULL,tfn3,NULL);
if(err!=0)
{
printf("can't create thread %sn",strerror(err));
exit(1);
}
err=pthread_cancel(tid3);
if(err!=0)
{
printf("can't cancel thread %sn",strerror(err));
exit(1);
}
err=pthread_join(tid3,NULL);
if(err!=0)
{
printf("can't join thread %sn",strerror(err));
exit(1);
}
return 0;
}
这段程序执行结果如下:
the first thread
the second thread
NO.2 clean up procdure
NO.2 clean up procdure
the third thread
NO.2 clean up procdure
ready to sleep
NO.2 clean up procdure
这里我有好几个问题:
1.在这个函数中,为什么有两个NULL,执行到第一个NULL处,线程已经退出,下面的两个函数(pthread_cleanup_pop(0);
)怎么会执行到?,如下:
void*tfn1(void*arg)
{
int a=1;
printf("the first threadn");
pthread_cleanup_push(cleanup,&a);
a=2;
pthread_cleanup_push(cleanup,&a);
return NULL;//
pthread_cleanup_pop(0);
pthread_cleanup_pop(0);
return NULL;//为什么有两个NULL,执行到第一个NULL处,线程已经退出,下面的两个函数(pthread_cleanup_pop(0);
)怎么会执行到?
}
2.执行结果中,出现:
the second thread
NO.2 clean up procdure
NO.2 clean up procdure
为什么不是:
the second thread
NO.2 clean up procdure
NO.1clean up procdure
这个执行结果似乎更合理。
请高手指教!多谢!
|
1. 没有执行到。你可以在pop下加printf试试看
2. 因为你push的参数是a的地址,应该为pthread_cleanup_push (cleanup, (void*)a);
cleanup中应为
printf ("NO.%d clean up procduren", (int) arg);
2. 因为你push的参数是a的地址,应该为pthread_cleanup_push (cleanup, (void*)a);
cleanup中应为
printf ("NO.%d clean up procduren", (int) arg);
|
A cancellation clean-up handler is popped from the stack and executed in the following circumstances:
1. When a thread is canceled, all of the stacked clean-up handlers are popped and executed in the reverse of the order in which they were pushed
onto the stack.
2. When a thread terminates by calling pthread_exit(3), all clean-up handlers are executed as described in the preceding point. (Clean-up han‐
dlers are not called if the thread terminates by performing a return from the thread start function.)
3. When a thread calls pthread_cleanup_pop() with a nonzero execute argument, the top-most clean-up handler is popped and executed.