先pthread_cancel()再pthread_join(),主线程退出?

cszdhhz 2011-10-19 11:47:01
代码在这:
http://q.cnblogs.com/q/29049/
...全文
325 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cszdhhz 2011-10-19
  • 打赏
  • 举报
回复
学习了
好了,MARK
qq120848369 2011-10-19
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 cszdhhz 的回复:]

这样确实可以了,我那个例子中
cout<< PTHREAD_CANCELED<<endl;
cout<<r<<endl;
cout<<"in main thread, tid = "<<pthread_self()<<endl;
这三行你输出了吗?我没有看到,所以认为它们没有执行。
你的意思是:这三个cout实际是运行了的,但是由于cout流错误,所以没有显示在屏幕上,对吗?
[/Quote]

对呀,我不是在7楼告诉你怎么检测了么。。。检测一下cout是false就说明流错误了。

cszdhhz 2011-10-19
  • 打赏
  • 举报
回复
这样确实可以了,我那个例子中
cout<< PTHREAD_CANCELED<<endl;
cout<<r<<endl;
cout<<"in main thread, tid = "<<pthread_self()<<endl;
这三行你输出了吗?我没有看到,所以认为它们没有执行。
你的意思是:这三个cout实际是运行了的,但是由于cout流错误,所以没有显示在屏幕上,对吗?
qq120848369 2011-10-19
  • 打赏
  • 举报
回复
#include <pthread.h>
#include <iostream>
#include <unistd.h>
#include <cstdio>

using namespace std;

void *thread(void *arg)
{
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
cout<<"in thread, tid = "<<pthread_self()<<endl;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
pause();

return NULL;
}

int main()
{
pthread_t tid;

if(pthread_create(&tid, NULL, thread, 0)==-1)
{
perror("create");
return 1;
}

if(pthread_cancel(tid)==-1)
{
perror("cancel");
return 2;
}

void *r;

if(pthread_join(tid, (void**)&r)==-1)
{
perror("join");
return 3;
}

cout<< PTHREAD_CANCELED<<endl;
cout<<r<<endl;

cout<<"in main thread, tid = "<<pthread_self()<<endl;

return 0;
}


这样更科学一点。
qq120848369 2011-10-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <unistd.h>
#include <cstdio>

using namespace std;

void *thread(void *arg)
{
cout<<"in thread, tid = "<<pthread_self()<<endl;
pause();

return NULL;
}

int main()
{
pthread_t tid;

if(pthread_create(&tid, NULL, thread, 0)==-1)
{
perror("create");
return 1;
}

sleep(1);

if(pthread_cancel(tid)==-1)
{
perror("cancel");
}

void *r;

if(pthread_join(tid, (void**)&r)==-1)
{
perror("join");
}

cout<< PTHREAD_CANCELED<<endl;
cout<<r<<endl;

cout<<"in main thread, tid = "<<pthread_self()<<endl;

return 0;
}


主函数加一句sleep 1把,原因是cancel中断了cout里的write操作,cout流出现了错误。

后边的代码是跑过的,不知道你怎么知道它没跑的 =,= .

执行完之后echo $?看到返回0了。

cszdhhz 2011-10-19
  • 打赏
  • 举报
回复
#include<pthread.h>
#include<iostream>
#include<unistd.h>

using namespace std;

void *thread(void *arg)
{
cout << "in thread, tid = " << pthread_self() << endl;

sleep(60);

return (void *)12;
}

int main()
{
pthread_t tid;
if(pthread_create(&tid, NULL, thread, 0) != 0)
{
cout << "pthread_create error" << endl;
return 0;
}

pthread_cancel(tid);

int *r;
pthread_join(tid, (void**)&r);

cout << PTHREAD_CANCELED << endl;
cout << r << endl;

cout << "in main thread, tid = " << pthread_self() << endl;
return 0;
}
qq120848369 2011-10-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 cszdhhz 的回复:]

感谢你的帮助!
但join()后无论什么语句,都执行不到
[/Quote]

真的假的。。。。我粘一份代码到csdn这里,那个网址里的代码没有换行,我没法弄。
cszdhhz 2011-10-19
  • 打赏
  • 举报
回复
感谢你的帮助!
但join()后无论什么语句,都执行不到
qq120848369 2011-10-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cszdhhz 的回复:]

我只想知道即使是所谓的“时序”问题,不论pthread_join返回值是什么,为什么不继续执行?
你什么版本的Linux?
[/Quote]

我没用你的程序,我自己写的程序,我用的C I/O,没用C++.

你在join之后加一句:

if(!cout)
{
write(STDERR_FILENO,"!",1);
return 1;
}

十有八九是对cout非线程安全操作导致cout异常了。
cszdhhz 2011-10-19
  • 打赏
  • 举报
回复
我只想知道即使是所谓的“时序”问题,不论pthread_join返回值是什么,为什么不继续执行?
你什么版本的Linux?
qq120848369 2011-10-19
  • 打赏
  • 举报
回复
if(pthread_create(&tid, NULL, thread, 0) != 0)

{ cout << "pthread_create error" << endl;


你老师肯定是在鄙视你这里呗,线程create之后你主线程也cout,子线程也在cout,你知道标准I/O都是带缓冲的,线程不安全。

要想完全安全起来,弄个互斥锁把输出语句括起来。
cszdhhz 2011-10-19
  • 打赏
  • 举报
回复
非虚拟机的 Ubuntu 11.10 也是一样
cszdhhz 2011-10-19
  • 打赏
  • 举报
回复
不行,我试了很多次
都是只打印“in thread, tid = 3079056240”
2L,我的想法和你一样,错了也要返回,然后继续执行后面的
老师说这个程序"时而正确,时而错误",让我考虑一下“时序”方面的因素
我想不通为什么
另外,我的环境是虚拟机Ubuntu
帅得不敢出门 2011-10-19
  • 打赏
  • 举报
回复
After a canceled thread has terminated, a join with that thread using pthread_join(3) obtains PTHREAD_CANCELED as the
thread's exit status. (Joining with a thread is the only way to know that cancellation has completed.)

另外你得判断下返回值,看下是不是出错了。

看man中的例子, 就算出错,后面的打印也会执行到。


qq120848369 2011-10-19
  • 打赏
  • 举报
回复
我打印了一下,退出状态是PTHREAD_CANCELED的.

pthread,tid:3076123504
Ret:4294967295,Canceld:4294967295
main,tid:3076130512

不知道你的为什么不打印.

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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