I wonder if there is a good way to terminate my process written in C++11 after a while?
In my process I have a main class with a pure virtual function run()
running the main program that could be blocked in communication processes. I want my run()
function to be forced to finish after a while (even if blocked) and the destructor of my main class (and all the destructors of the members) to be called.
Now I have a timer that call std::terminate
via a callback.
namespace Timer
{
void start(Duration time, function<void()> task)
{
thread([time, task]() {
this_thread::sleep_for(time);
task();
}).detach();
}
}
run()
function will not return, you cannot end the program cleanly. Maybe you could runrun()
in a thread, and callthread::detach()
on it: main destructors will be called, but I wouldn't call it clean.wait_for
or similar.run
in a thread. If it doesn't finish in time, just do a normal exit. You will get everything aboverun
shut down properly. You don't control things belowrun
, there's no hope of making them exit gracefully anyway.