Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. For a junior developer, is there a set of rules for what to do and what to avoid when using std::async?

  2. What are the problems presented in this video? Are they related to this article?

like image 856
NoSenseEtAl Avatar asked Sep 20 '12 08:09

NoSenseEtAl


People also ask

What is std :: async?

The function template async runs the function f asynchronously (potentially in a separate thread which might be a part of a thread pool) and returns a std::future that will eventually hold the result of that function call. 1) Behaves as if (2) is called with policy being std::launch::async | std::launch::deferred.

What does async do in C++?

As the name indicates, C++ async is a function template fn, which takes functions or function objects as arguments (basically called callbacks) and runs them asynchronously. It returns the std:: the future object which is used to keep the result of the above function. The result is stored in the shared state.

Does STD async use threads?

Example# std::async is also able to make threads. Compared to std::thread it is considered less powerful but easier to use when you just want to run a function asynchronously.

Does STD async create new thread?

One nice thing about std::async is that it manages a thread pool under the hood. So there is no worry that every time we invoke std::async a new thread is launched.


1 Answers

There are several issues:

  1. 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.

  2. 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.

  3. 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.

  4. 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:

  • Use std::async with the default launch policy.
  • Make sure you explicitly wait for all your futures.
  • Don't use thread-local storage in the async tasks.
like image 165
Anthony Williams Avatar answered Sep 21 '22 00:09

Anthony Williams