Threads and signals

Daniel Eischen deischen at freebsd.org
Fri Jan 4 11:35:41 PST 2008


On Thu, 3 Jan 2008, Ivan Voras wrote:

> Hi,
>
> How do threads interact with signals? In particular, if I have a "main"
> process thread (the one started by main()) which generates items for a
> mutex-protected queue which are consumed by a worker thread, and I need
> to insert an item in the queue from the signal handler, am I correct
> that doing pthread_mutex_lock() from the signal handler could deadlock
> if the signal handler is executed by any of the threads (and the mutex
> is non-recursive)?

Yes, you don't want to call pthread_mutex_lock() (or really any other
non-async-signal-safe function) from the signal handler.

> How is this solved in general? By recursive mutexes?

You can use sigwait() to wait for the signal and block it in all
other threads, or you can use a pipe and write(2) to the pipe from
the signal handler and have another reader thread handle the request.
I suppose there are other ways, like sem_post().

-- 
DE


More information about the freebsd-threads mailing list