libev signal mask and thread

Konstantin Osipov kostja at
Sat Apr 28 16:47:31 CEST 2012

* Marc Lehmann <schmorp at> [12/04/28 16:28]:
> > How exactly does libev work with the process signal mask?
> Exactly as documented - if you care for undocumented details, you could
> look at the source, but these details *could* change.

Sorry, I meant the thread signal mask.
Signals, together with threads, are difficult, so even after
reading the manual I didn't quite understand what's the best thing
to do. Please see below. 

> > Is any manipulation with the thread signal mask in created threads
> > necessary to ensure that signals actually arrive to the thread
> > which is running the libev loop with appropriate watchers?
> No, signals in posix are per-process, so you don't have to do
> anything. Look at EVFGLAG_NOSIGMASK though, because libev might call
> sigprocmask on it's own, which is not allowed in a threaded program (but
> works on many platforms).

The manual describes an approach to signal handling with

I would prefer, however, to use ev_signal callbacks normally in my
event loop, without having to manually feed them signals.

In other words, rather than blocking signals in the main thread,
which is running ev_loop, I'd rather block them in all other threads in my
application, to make sure they get delivered to the main thread.
Unless I block signals in all threads but the main, the signal
sent to a process can be delivered to any thread, and thus libev
won't see it.

I was wondering what would be a canonical way to do it? 

A simple approach is to temporarily block signals by calling
pthread_sigmask(), then call pthread_create(), make sure the created
thread inherits the signal mask, and then unblock signals
again and let libev handle them.

Will that work?

-- - an efficient, extensible in-memory data store

More information about the libev mailing list