Tuesday, May 5, 2009

work around on pthread_cancel for android boinic

after two days research on pthread_cancel, I found android boinic libc doesn't support it.

currently I'm designing a timer to handle pthread lifecycle, each pthread in pool has its own timer, when timer out, orignally I want to use pthread_cancel to let the that pthread die, but I found pthread_cancel has several drawbacks, such it will produce the dead lock when the pthread is mutexed, and must be careful to deal with this kind of condition. another one is android boinic libc doesn't support it.

Then I use pthread_kill to trigger a SIG_USR1 signal and use signal handler to exit this pthread
and tried it, it works, but still wondering if any drawbacks for this kind of method.
Timer out:

if ( (status = pthread_kill(pthread_id, SIGUSR1)) != 0)
printf("Error cancelling thread %d, error = %d (%s)", pthread_id, status, strerror status));

USR1 handler:

struct sigaction actions;
memset(&actions, 0, sizeof(actions));
actions.sa_flags = 0;
actions.sa_handler = thread_exit_handler;
rc = sigaction(SIGUSR1,&actions,NULL);
void thread_exit_handler(int sig)
printf("this signal is %d \n", sig);


  1. The greatness of a man is not in how much wealth he acquires, but in his integrity and his ability to affect those around him positively. See the link below for more info.


  2. I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very
    useful and knowledgeable. I would like to thank you for the effort you have made in writing this article.