views:

25

answers:

1

How do I cause a thread to respond to pthread_cancel() if it is blocked on a sem_wait()?

+2  A: 

You can set a signal handler, let's says for SIGUSR1, for the thread that will be blocking then, from another thread, you call pthread_cancel(tid) and pthread_kill(tid, SIGUSR1). The thread will be canceled once sem_wait() is interrupted by the signal.

Gonzalo
Beware that you don't actually set signal handlers per thread, you set them per process. You can set signal masks per thread to control delivery of asynchronous signals sent to the process, though. Mainly I point this out because it' a common mistake to unregister the handler when a thread exits, but that will remove the handler for every thread.
Ben Jackson