高手进,关于pthread_cleanup_push的问题

xukai6571186 2010-12-28 04:53:33
有哪位大侠能整理一下pthread_cleanup_push指定函数不被执行的诸多情况。

目前遇到这样一问题,起一线程,在线程函数最开始加了一个pthread_cleanup_push(freemylock,NULL);在函数结束的地方加了一个pthread_cleanup_pop(0);线程函数又调了其他的函数,用pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);将线程设置成可以取消。此时在线程结束之前我在主线程里pthread_cancel(pthreadid),这时候子线程被中止了,可是我设置的清理函数freemylock()却没被执行。哪位大侠能帮帮我呀,我写的一个测试程序和我实际的程序逻辑是一样的可以执行清理函数,测试程序如下:
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

void inf_lop2()
{
int w = 0;
while(++w)
{
printf("ddddddddddddddddddddddd\n");
if(w>10000)
{
break;
}
}
}
void inf_loop()
{
int i =0;
while (1)
{
i++ ;
inf_lop2();
// printf("i is %d\n",i);
// sleep(1);
// printf("mmmmmmmmmmmmmmmmmmmmmmmm\n");
}
printf("%d", i);
}

void cleanup(void* arg){
printf("cleanup: wwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
}

void* thr_fn1(void* arg){
printf("thread1 starte\n");
pthread_cleanup_push(cleanup, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);

printf("thread 1 push complete \n");
while(1)
{
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);

printf("11111111111111111111111111\n");
inf_loop();
printf("222222222222222222222222222222\n");
pthread_testcancel();
printf("33333333333333333333\n");
}

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


int main(void){
int err;
pthread_t tid1, tid2;
void *tret;

err = pthread_create(&tid1, NULL, thr_fn1, (void*) 1);

getchar();
printf("thread 1 exit code %d\n", (int)tret);
pthread_cancel(tid1);
printf("11111111111\n");
pthread_join(tid1,NULL);
printf("qqqqqqqqqqqqq\n");
sleep(3);
printf("############################\n");
printf("thread 2 exit code %d\n", (int)tret);
getchar();
return 0;
}



可在我的工程里就是不执行,到底怎么回事?由于工程太大无法贴出代码望大侠原谅。不知我说明白了没
...全文
178 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
海枫 2010-12-31
  • 打赏
  • 举报
回复
[Quote=引用楼主 xukai6571186 的回复:]
有哪位大侠能整理一下pthread_cleanup_push指定函数不被执行的诸多情况。

目前遇到这样一问题,起一线程,在线程函数最开始加了一个pthread_cleanup_push(freemylock,NULL);在函数结束的地方加了一个pthread_cleanup_pop(0);线程函数又调了其他的函数,用pthread_setcancelstate(PTHREAD_CANCEL_……
[/Quote]

void* thr_fn1(void* arg){
printf("thread1 starte\n");
pthread_cleanup_push(cleanup, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);

printf("thread 1 push complete \n");
while(1)
{
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);

printf("11111111111111111111111111\n");
inf_loop();
printf("222222222222222222222222222222\n");
pthread_testcancel();
printf("33333333333333333333\n");
}

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

inf_loop函数是一个deadloop吧,它里面只调用printf,好像这个函数不是一个cancel point,所以它永远不会执行到pthread_testcancel().

因为不能执行到cancel point,所以pthread_cancel函数,不能对线程做什么。

所以程序不能退出吧?
justkk 2010-12-29
  • 打赏
  • 举报
回复
需要确认一下pthread_cleanup_pop是否执行了

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧