Defined in header
<csignal> | ||
---|---|---|
void (*signal( int sig, void (*handler) (int))) (int); |
Sets the error handler for signal sig
. The signal handler can be set so that default handling will occur, signal is ignored, or a user-defined function is called.
When signal handler is set to a function and a signal occurs, it is implementation defined whether std::signal(sig, SIG_DFL)
will be executed immediately before the start of signal handler. Also, the implementation can prevent some implementation-defined set of signals from occurring while the signal handler runs.
For some of the signals, the implementation may call std::signal(sig, SIG_IGN)
at the startup of the program. For the rest, the implementation must call std::signal(sig, SIG_DFL)
.
sig | - | the signal to set the signal handler to. It can be an implementation-defined value or one of the following values:
|
|||
handler | - | the signal handler. This must be one of the following:
|
Previous signal handler on success or SIG_ERR
on failure (setting a signal handler can be disabled on some implementations).
The following limitations are imposed on the user-defined function that is installed as a signal handler.
If the user defined function returns when handling SIGFPE
, SIGILL
, SIGSEGV
or any other implementation-defined signal specifying a computational exception, the behavior is undefined.
If the signal handler is called as a result of std::abort
or std::raise
, the behavior is undefined if the signal handler calls std::raise
.
If the signal handler is called NOT as a result of std::abort
or std::raise
, the behavior is undefined if.
std::abort
std::_Exit
std::quick_exit
std::signal
with the first argument being the number of the signal currently handled (async handler can re-register itself, but not other signals). std::atomic
(since C++11) or volatile std::sig_atomic_t
. On entry to the signal handler, the state of the floating-point environment and the values of all objects is unspecified, except for.
volatile std::sig_atomic_t
std::atomic
types (since C++11) std::atomic_signal_fence
(since C++11) On return from a signal handler, the value of any object modified by the signal handler that is not volatile std::sig_atomic_t
or std::atomic
is undefined.
The behavior is undefined if std::signal
is used in a multithreaded program. It is not required to be thread-safe.
POSIX requires that signal
is thread-safe, and specifies a list of async-signal-safe library functions that may be called from any signal handler.
Signal handlers are expected to have C linkage and, in general, only use the features from the common subset of C and C++. It is implementation-defined if a function with C++ linkage can be used as a signal handler.
#include <csignal> #include <iostream> namespace { volatile std::sig_atomic_t gSignalStatus; } void signal_handler(int signal) { gSignalStatus = signal; } int main() { // Install a signal handler std::signal(SIGINT, signal_handler); std::cout << "SignalValue: " << gSignalStatus << '\n'; std::cout << "Sending signal " << SIGINT << '\n'; std::raise(SIGINT); std::cout << "SignalValue: " << gSignalStatus << '\n'; }
Possible output:
SignalValue: 0 Sending signal 2 SignalValue: 2
runs the signal handler for particular signal (function) |
|
C documentation for signal |
|
(C++11)
| fence between a thread and a signal handler executed in the same thread (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/program/signal