When trying to answer another Stackoverflow question, I realized that this simple C++11 snippet is implicitly blocking the calling thread:
std::async(std::launch::async, run_async_task)
To me this would have seemed the canonical C++11 way to launch a task asynchronously without caring about the result. Instead one has to apparently explicitly create and detach a thread (see answer to mentioned question) in order to achieve this.
So here's my question: Is there any reason in regards to safety/correctness that the destructor of a std::future
has to be blocking? Wouldn't it be enough if it blocks on get
only and otherwise, if I'm not interested in the return value or exception, it's simply fire and forget?
Blocking destructors of futures returned by std::async and of threads: That's a controversial topic. The following list of papers in chronological order reflects some of the discussions by the members of the committee:
Although there was a lot of discussion, there are no changes planned for C++14 regarding the blocking behaviour of the destructors of std::future and std::thread.
Regarding your question, the most interesting paper is probably the second by Hans Boehm. I quote some parts to answer your question.
Update: Michael Wong's Trip Report also contains some interesting information regarding the outcomes of the meeting in September 2013: