5

I was reading the description of std::async at cppreference.com. The first description says :

The template function async runs the function f asynchronously (potentially in a separate thread which may be part of a thread pool) and returns a std::future that will eventually hold the result of that function call.

. [cppreference link]: std::async

What is the thread pool cppreference.com is talking about ?

I read the standard draft N4713 (C++ 17) and there is no mention of a possible thread pool usage. I also know that there is no thread pool in the standard C++ as of now.

cyrildz
  • 73
  • 7

2 Answers2

4

Purely hypothetical. cppreference is trying to tell you that standard allows execution of the task in the thread pool (as opposed to launching a new thread to execute it). And although standard may not explicitly allow it, there is nothing which would prohibit it either.

I am not aware of any implementation which would use a thread pool for std::async.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • So If get it, it is message addressed to the compiler implementer ? Because now I'm currious on how I could tell std::async to use a dedicated thread pool – cyrildz Apr 26 '19 at 18:24
  • Could be. I am not really sure what's the significance of this note. – SergeyA Apr 26 '19 at 18:26
  • The significance of this note is that if your function `f` calls `std::this_thread::get_id()`, it may get the same `id` back for different executions. – Raymond Chen Apr 26 '19 at 18:33
  • 1
    @RaymondChen that could be the case even if it is executed on the new thread for every launch. – SergeyA Apr 26 '19 at 18:48
  • 1
    @RaymondChen I'm ok with the part where cppreference says that f could potentially be run on a separate thread,which we as user can control by setting the launch policy to std::launch::async or std::launch::deffered. But for the actual thread which run f to be part of a thread pool it means that somehow std::async can be instructed to use a thread pool. Sow how do we get that ? – cyrildz Apr 26 '19 at 18:48
  • @cyrildz may be I am not clear enough. You can't direct `std::async` to use thread pool or not use the thread pool. It is decided by implementation (and the decision right now is **not** using the pool) – SergeyA Apr 26 '19 at 18:49
  • @SergeyA No worry , I'm fine with your answer. It makes sense to me. I was just not convinced by the comment from RaymondChen – cyrildz Apr 26 '19 at 18:53
  • 1
    I've written a program that initially used thread creation/join for accumulating results from a lot of FFT processing. I initially saw only a 2x speedup on my 6 core machine. But after changing the code to use async, I saw about a 5x increase in the multi-threaded part of the code. I chalk this up to async using a thread pool so the overhead of thread creation/destruction is avoided. Windows 10 x64 with VS2017. – doug Apr 26 '19 at 19:25
  • @SergeyA it also means that your `thread_local` variables may be shared between multiple invocations of `f`. – Raymond Chen Apr 26 '19 at 19:29
  • _And although standard may not explicitly allow it, there is nothing which would prohibit it either._ Yes there is, please see my answer. – Paul Sanders Dec 10 '21 at 10:47
4

cppreference and the C++ standard are in fact at odds about this. cppreference says this (emphasis and strikethrough mine):

The template function async runs the function f asynchronously (potentially optionally in a separate thread which may be part of a thread pool).

Whereas the C++ standard says this:

If launch::async is set in policy, [std::async] calls [the function f] as if in a new thread of execution ...

And these are clearly two different things.

Only Windows' implementation of std::async uses a thread pool AFAIK, while gcc and clang start a new thread for every invocation of std::async (when launch::async is set in policy), and thus follow the standard.

More analysis here: https://stackoverflow.com/a/50898570/5743288

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48