Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 4 Jan 2008 14:35:39 -0500 (EST)
From:      Daniel Eischen <deischen@freebsd.org>
To:        Ivan Voras <ivoras@freebsd.org>
Cc:        freebsd-threads@freebsd.org
Subject:   Re: Threads and signals
Message-ID:  <Pine.GSO.4.64.0801041432530.775@sea.ntplx.net>
In-Reply-To: <fljin9$crb$1@ger.gmane.org>
References:  <fljin9$crb$1@ger.gmane.org>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.GSO.4.64.0801041432530.775>