Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use std::async vs std::threads?

Can anybody give a high level intuition about when to use each of them?

References:

  • Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?
  • When is it a good idea to use std::promise over the other std::thread mechanisms?
like image 693
Javi Avatar asked Sep 12 '14 18:09

Javi


People also ask

Does STD async use threads?

If the async flag is set, then a callable function will be executed in a separate thread. If the deferred flag is set, a callable function will be stored together with its arguments, but the std::async function will not launch a new thread.

Does STD async create new thread?

= 0), then async converts f and args... the same way as by std::thread constructor, but does not spawn a new thread of execution.

How does C++ async work?

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 C++ support asynchronous programming?

C++ coroutines can also be used for asynchronous programming by having a coroutine represent an asynchronous computation or an asynchronous task.


2 Answers

It's not really an either-or thing - you can use futures (together with promises) with manually created std::threads. Using std::async is a convenient way to fire off a thread for some asynchronous computation and marshal the result back via a future but std::async is rather limited in the current standard. It will become more useful if the suggested extensions to incorporate some of the ideas from Microsoft's PPL are accepted.

Currently, std::async is probably best suited to handling either very long running computations or long running IO for fairly simple programs. It doesn't guarantee low overhead though (and in fact the way it is specified makes it difficult to implement with a thread pool behind the scenes), so it's not well suited for finer grained workloads. For that you either need to roll your own thread pools using std::thread or use something like Microsoft's PPL or Intel's TBB.

You can also use std::thread for 'traditional' POSIX thread style code written in a more modern and portable way.

Bartosz Milewski discusses some of the limitations of the way std::async is currently specified in his article Async Tasks in C++11: Not Quite There Yet

like image 54
mattnewport Avatar answered Sep 19 '22 07:09

mattnewport


One simple reason I've found is the case when you want a way to detect (via polling) whether an asynchronous job is done. With std::thread, you have to manage it yourself. With std::async you can query std::future::valid() (or use std::future::wait_for/wait_until(...)) to know when it is done.

like image 26
Robert Avatar answered Sep 19 '22 07:09

Robert