What is the issue with std::async?
Near the beginning of this clip from C++ And Beyond, I heard something about problems with std::async
. I have two questions:
For a junior developer, is there a set of rules for what to do and what to avoid when using std::async
?
What are the problems presented in this video? Are they related to this article?
There are several issues:
std::async
without a launch policy lets the runtime library choose whether to start a new thread or run the task in the thread that called get()
or wait()
on the future. As Herb says, this is the case you most likely want to use. The problem is that this leaves it open to the QoI of the runtime library to get the number of threads right, and you don't know whether the task will have a thread to itself, so using thread-local variables can be problematic. This is what Scott is concerned about, as I understand it.
Using a policy of std::launch::deferred
doesn't actually run the task until you explicitly call get()
or wait()
. This is almost never what you want, so don't do that.
Using a policy of std::launch::async
starts a new thread. If you don't keep track of how many threads you've got, this can lead to too many threads running.
Herb is concerned about the behaviour of the std::future
destructor, which is supposed to wait for the task to complete, though MSVC2012 has a bug in that it doesn't wait.
For a junior developer, I would suggest:
std::async
with the default launch policy. I could not disagree more about the advice to use the default policy.
If you go through the pain of designing independent computation units, you probably don't expect them to run sequentially while half a dozen CPUs twiddle their thumbs, which can "legally" happen depending on the compiler you choose.
The implicit assumption of the default behaviour is that some sophisticated thread pool mechanism will optimize task placement (possibly letting some run sequentially on the caller's CPU), but that is pure phantasy, since nothing specifies what the C++ runtime must do (which would go way beyond the scope of a compiler runtime anyway).
This looks more like undefined behaviour by design to me.
A class named "async" should launch asynchronous execution units, unless some explicit and deterministic behaviour parameter tells it otherwise.
Frankly, except for debugging purpose, I can't see a use for launch::deferred
, unless you plan on writing your own pseudo-scheduler, in which case you'll be better off using plain threads.
So my advice would be to specify launch::async
when you use async
, (telling the compiler something like "hey, I want some async task, but really async, ok?") and not to use async
at all if you just want to execute tasks sequentially.
If you run into trouble with your async tasks, it can be convenient to revert to deferred
policy to debug them more easily, but that's about it.
下一篇: std :: async有什么问题?