57

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?

6
  • Just replied your comment and here you have a question lol
    – SwiftMango
    May 4, 2014 at 10:26
  • 1
    There was a lot of discussion on this behaviour. Take a look at the following documents: open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3773.pdf and open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3776.pdf
    – nosid
    May 4, 2014 at 10:29
  • 5
    @texasbruce: Yes, because it really bugs me. I was sure this is the new C++11y way to do it and I was really suprised to see it's blocking even if I don't care about the result (only mentioned in one note at the end). Unless there's a clear necessity for that I don't think it's a good interface. May 4, 2014 at 10:30
  • 2
    I believe that's one of the changes proposed to c++14 to do it as you expected.
    – W.B.
    May 4, 2014 at 10:32
  • @W.B.: AFAIK one proposal is to introduce the then method so you can combine futures in an asynchronous way. I would hope they would also fix this behaviour at that point, too. May 4, 2014 at 10:37

1 Answer 1

46

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.

N3679: Async() future destructors must wait

[..] Futures returned by async() with async launch policy wait in their destructor for the associated shared state to become ready. This prevents a situation in which the associated thread continues to run, and there is no longer a means to wait for it to complete because the associated future has been destroyed. Without heroic efforts to otherwise wait for completion, such a "run-away" thread can continue to run past the lifetime of the objects on which it depends.

[Example]

The end result is likely to be a cross-thread "memory smash". This problem is of course avoided if get() or wait() is called [..] before they [the futures] are destroyed. The difficulty [..] is that an unexpected exception may cause that code to be bypassed. Thus some sort of scope guard is usually needed to ensure safety. If the programmer forgets to add the scope guard, it appears likely that an attacker could generate e.g. a bad_alloc exception at an opportune point to take advantage of the oversight, and cause a stack to be overwritten. It may be possible to also control the data used to overwrite the stack, and thus gain control over the process. This is a sufficiently subtle error that, in our experience, it is likely to be overlooked in real code.

Update: Michael Wong's Trip Report also contains some interesting information regarding the outcomes of the meeting in September 2013:

The View from the C++ Standard meeting September 2013 Part 2 of 2.

On the issue that async destructors should not block we devoted a great deal of discussion on it. [..] The only position that received considerable support was [..] giving advisory that future destructors will not block, unless returned from async, making it the notable exception. [..] After significant discussion, the only part that we tried to carry was N3776, an attempt to clarify the position that ~future and ~shared_future don’t block except possibly in the presence of async. There was an attempt to issue a deprecation along the lines of C. Deprecate async without replacement. This motion was actually almost put forward. But [..] it died even before it reached the operating table.

8
  • 4
    Great, exactly the kind of answer I was looking for! Still not convinced it was the right decision, but at least the trade-offs are clear to me now. I guess it's some extreme form of RAII, like making sure a file handler is closed in the destructor. May 4, 2014 at 20:16
  • 1
    About it being fixed in C++14, this proposal sounds to me like it would: n3773. Or at least it gives you the possibility to manually 'detach'. May 4, 2014 at 20:17
  • 2
    I was sure I have seen some information on the outcome of the discussions. Now I have found it and updated the answer.
    – nosid
    May 4, 2014 at 21:32
  • Although the behaviour won't be changed, the concurrency TS's std::future::then() looks like it could be used here. The std::future returned by then doesn't seem to be the same kind, so it doesn't need to block if it's destroyed. std::async( []{ sleep(20); } ).then( []{} ); return;
    – TBBle
    Jan 14, 2016 at 15:12
  • 5
    It wasn't clear from the above, but N3776 went into C++14.
    – TBBle
    Jan 15, 2016 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.