I was testing the behavior of how pthread_cancel works.
#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;
}
I expected that
(1) when cancallation is disabled, any call to pthread_cancel will be ignored but the call shall be remembered
(2) until the cancellation is enabled: it will check if there's previous call to pthread_cancel, and if yes, a cancellation is done.
sleep 1
sleep 1
cancel thread
sleep 1
sleep 1
sleep 1
sleep 1
But on my linux server, it prints:
sleep 1
sleep 1
cancel thread
sleep 1
sleep 1
sleep 1
FATAL: exception not rethrown
sleep 1
Aborted (core dumped)
Just having no idea what actuall happened, and how the Fatal exception is throw? I should have some wrong understandings. Need your suggestions!