sigaction, muxex, condition variable

  • Thread starter Mike - EMAIL IGNORED
  • Start date
M

Mike - EMAIL IGNORED

In a Posix compliant multi-threaded C++ program,
I would like to use sigaction() to catch SIGTERM,
and from my signal handler, lock a mutex, and
broadcast a condition variable. Is this ok?

Thanks,
Mike.
 
V

Victor Bazarov

Mike said:
In a Posix compliant multi-threaded C++ program,
I would like to use sigaction() to catch SIGTERM,
and from my signal handler, lock a mutex, and
broadcast a condition variable. Is this ok?

You might be better off asking in a Posix newsgroup.
 
J

James Kanze

Mike - EMAIL IGNORED wrote:
You might be better off asking in a Posix newsgroup.

You're probably right, but there are two C++ questions hidden in
there. The answer to the first is, of course, he can only call
these functions from C++ if the system has taken the necessary
steps so that they will show up as `extern "C"' when included
from a C++ program. (All the systems I know have, but you never
know.) The answer to the second is a bit more direct: according
to the C standard (and this part of the C standard has been
explicitly included by reference in the C++ standard): "If the
signal occurs other than as the result of calling the abort or
raise function, the behavior is undefined if the signal handler
refers to any object with static storage duration other than by
assigning a value to an object declared as volatile
sig_atomic_t, or the signal handler calls any function in the
standard library other than the abort function, or the signal
function with the first argument equal to the signal number
corresponding to the signal that caused the invocation of the
handler." Of course, an implementation may define any undefined
behavior, and Posix does, in fact, define a few additional
things you can do in a signal handler.

There's also a more general programming question involved, more
appropriate to comp.threads than any system specific group. But
the answer is obvious: any attempt to lock a mutex (or acquire a
semaphore, or anything else) from an asynchronous signal
guarantees a potential deadlock. (FWIW: under Posix, you do not
normally use signal handlers in multithreaded code. Posix has
other ways of handling this, that do, or at least can be made to
work.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top