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));
sigemptyset(&actions.sa_mask);
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);
pthread_exit(0);
}

No comments:

Post a Comment