我正在测试pthread_cancel的工作方式。linux上的pthread_cancel()会导致exception/coredump,为什么?
#include<pthread.h>
#include<unistd.h>
#include<iostream>
using namespace std;
int retval=70;
void* tf(void*arg){
int oldstate;
int i=0;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
while(true){
cout<<"sleep 1"<<endl;
sleep(1);
++i;
if(i==5){//response to last pthread_cancel()?
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
}
}
return NULL;
}
int main(){
pthread_t tid;
pthread_create(&tid,NULL,tf,NULL);
sleep(2);
pthread_cancel(tid);//not responded until "PTHREAD_CANCEL_ENABLE"?
cout<<"cancel thread"<<endl;
pthread_join(tid,NULL);
return 0;
}
我预计
(1)当cancallation被禁用时,pthread_cancel可以任意的通话将被忽略,但通话将被铭记
(2),直至取消已启用:它会检查是否有先前的pthread_cancel调用,如果是,则取消完成。
sleep 1
sleep 1
cancel thread
sleep 1
sleep 1
sleep 1
sleep 1
但我的Linux服务器上,它打印:
sleep 1
sleep 1
cancel thread
sleep 1
sleep 1
sleep 1
FATAL: exception not rethrown
sleep 1
Aborted (core dumped)
只要有不知道发生了什么实际工作,以及严重异常是怎么扔? 我应该有一些错误的理解。需要你的建议!
与你的问题无关,但是如果你声明了一个函数来返回一些东西,你应该真的返回一些东西,即使它只是一个简单的'return NULL;' –
只修复了缺失的返回值-----仍然是我所描述的同样的问题。 –