Thread Cancellation in GNU/Linux:
Today, I'm interested to explore how we can prepare thread in GNU/Linux to ignore thread cancellation. This is acvhieved with pthread_setcancelstate().
The signature of the function is:
int pthread_setcancelstate(int state, int *oldstate);
The function is thread and signal safe. The function sets the cancel state to one of PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE and returns the old cancel state.
Please see the second parameter.
void *Print_Details(void *param)
{
printf("This is secondary thread's entry...\n");
int oldState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
sleep(1);
for(int i = 0; i < 10; ++i)
{
printf("Inside the secondary thread...\n");
}
pthread_setcancelstate(oldState, NULL);
printf("This is secondary thread's exit...\n");
return NULL;
}
/* Main program */
int main()
{
pthread_t thread;
int thread_ret;
void *status;
printf("This is main... Creating thread with default param\n");
pthread_create(&thread, NULL, &Print_Details, NULL);
sleep (1);
// Called secondary thread cancellation....
printf("Called secondary thread cancellation...\n");
pthread_cancel(thread);
printf("Thread Cancel ignored...\n");
sleep(2);
printf("Main exited\n");
return 0;
}
Output:
This is main... Creating thread with default param
This is secondary thread's entry...
Called secondary thread cancellation...
Thread Cancel ignored...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
This is secondary thread's exit...
Main exited
Today, I'm interested to explore how we can prepare thread in GNU/Linux to ignore thread cancellation. This is acvhieved with pthread_setcancelstate().
The signature of the function is:
int pthread_setcancelstate(int state, int *oldstate);
The function is thread and signal safe. The function sets the cancel state to one of PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE and returns the old cancel state.
Please see the second parameter.
void *Print_Details(void *param)
{
printf("This is secondary thread's entry...\n");
int oldState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
sleep(1);
for(int i = 0; i < 10; ++i)
{
printf("Inside the secondary thread...\n");
}
pthread_setcancelstate(oldState, NULL);
printf("This is secondary thread's exit...\n");
return NULL;
}
/* Main program */
int main()
{
pthread_t thread;
int thread_ret;
void *status;
printf("This is main... Creating thread with default param\n");
pthread_create(&thread, NULL, &Print_Details, NULL);
sleep (1);
// Called secondary thread cancellation....
printf("Called secondary thread cancellation...\n");
pthread_cancel(thread);
printf("Thread Cancel ignored...\n");
sleep(2);
printf("Main exited\n");
return 0;
}
Output:
This is main... Creating thread with default param
This is secondary thread's entry...
Called secondary thread cancellation...
Thread Cancel ignored...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
Inside the secondary thread...
This is secondary thread's exit...
Main exited
Comments