我正在測試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;' –
只修復了缺失的返回值-----仍然是我所描述的同樣的問題。 –