The IceUtil::CtrlCHandler class provides a portable mechanism to handle Ctrl+C and similar signals sent to a C++ process. On Windows,
IceUtil::CtrlCHandler is a wrapper for
SetConsoleCtrlHandler; on POSIX platforms, it handles
SIGHUP,
SIGTERM and
SIGINT with a dedicated thread that waits for these signals using
sigwait. Signals are handled by a callback function implemented and registered by the user. The callback is a simple function that takes an
int (the signal number) and returns
void; it should not throw any exception:
Constructs an instance with a callback function. Only one instance of CtrlCHandler can exist in a process at a given moment in time. On POSIX platforms, the constructor masks
SIGHUP,
SIGTERM and
SIGINT, then starts a thread that waits for these signals using
sigwait. For signal masking to work properly, it is imperative that the
CtrlCHandler instance be created before starting any thread, and in particular before initializing an Ice communicator.
A typical use for CtrlCHandler is to shutdown a communicator in an Ice server (see
Section 8.3.1).