I am facing one issue related to pthread_cancel. Please see the code below:
void* func(void *arg)
{
while(1)
{
sleep(2);
}
}
#include<stdlib.h>
#include <stdio.h>
#include <pthread.h>
int main()
{
void *status;
pthread_t thr_Var;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
pthread_create(&thr_Var,NULL,func,NULL);
pthread_cancel(thr_Var);
pthread_join(thr_Var,&status);
return 0;
}
My doubt is even if i disable the cancel state, still pthread_cancel is working and thread is getting terminated.
Any help will be appreciated
pthread_setcancelstatesets the cancelability type of the calling thread, i.e. the main thread in your case. So if you want to make the newly created thread non-cancelable you should call that function from within the context of that thread.See man 3 pthread_setcancelstate
Note that while Linux pthreads implementation permits a NULL
oldstatepointer, POSIX, however, does not specify that, so it’s best to provide a pointer foroldsate.