There is no portable way to handle multiple calls to exit () - because this is undefined (behavior), which happens in this case.
But for a specific platform, you can find a way to do this. A somewhat common solution for โcalled multiple timeโ is to have a flag in your static objects, such as โI'm already destroyed.โ As usual, you can hide this in the template:
template <typename T> class StaticExitHandled { public: std::unique_ptr<T> t_; ~StaticExitHandled() { t_.release(); } };
Now just remember the declaration of all your static objects with this template. This is just the gist of this, add the bells and whistles to your liking. Alternatively, instead of std :: unique_ptr <> you can use boost :: optional <> or some such things.
I do not think there is a general solution for "not called at all."
Actually, I would advise not to have non-trivial static objects in a multi-threaded environment. Thus, there are only static PODs and objects with very limited destructors (depending on what is safe to do at this point in your environment, that is, closing file descriptors is okay in most cases).
source share