There is a C++ structure having static instance:
class A
{
public:
A () {}
~A() {
// smth.
}
} a;
Does pkill of process calls destructors of all static objects implemented in C++?
Is there a guarentee that pkill, before process termination will call destructor of a?
It depends.
pkill, likekill, sends a signal to the process. By default it sendsSIGTERM, but it can send any signal you tell it to.The default handler for
SIGTERMterminates the process immediately, without running object destructors or functions registered withatexit. You can change this behavior by registering your own signal handler using thesignalorsigactionfunctions. If you want to let the rest of your program clean up gracefully, you can have astd::atomic<bool>that the rest of your program periodically checks to determine if it should continue working.Note that you are fairly limited in what you can safely do in a signal handler. Linux, for example, has a list of async-signal-safe functions that you can safely call. C++17 also specifically specifies that member functions of
std::atomictypes are safe if the type is lock-free. That means that you should not simply callstd::exitdirectly from a signal handler, since that will execute objects' destructors in the context of the signal handler, which will likely perform unsafe operations.