I was looking for a way to stop a thread that perform a task every 2 seconds. I decided to try to use a std::promise/future so that the thread can exit immediately when the promise is set.
#include <future>
#include <iostream>
#include <chrono>
#include <csignal>
std::promise<void> stop;
int main() {
std::signal(SIGINT, [] (int) { stop.set_value(); } );
auto future = stop.get_future();
while (future.wait_for(std::chrono::seconds(1)) != std::future_status::ready) {
std::cout << "I'm still there" << std::endl;
}
}
Actually this does not work and crashes that way:
$ ./a.out I'm still there ^Cterminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1 Abandon (core dumped)
Ok, One should take care of what he does in handler context, but I must say I was not expecting this crash; and I don't really understand it... Do you have any idea?