Std :: async does not start new threads using std :: launch :: async policy

If I run std :: async with std :: launch :: async, should I not run each asynchronous task in a new thread? At this point, it looks like new async tasks are moving to the thread that has just completed its work. I am using VC11 as my compiler. As you can see from the output, when a new worker (for example, a worker receives a stream with ID 34500 several times) is started using std :: async, it starts in a previously completed stream. Is my understanding of std :: async wrong or is there a hidden job to steal a queue or something like that?

Worker (ID=24072) starting. Worker (ID=34500) starting. Worker (ID=32292) starting. Worker (ID=31392) starting. Worker (ID=17976) starting. Worker (ID=31580) starting. Worker (ID=33512) starting. Worker (ID=33804) starting. Worker 32292 finished. Worker (ID=32292) starting. Worker 17976 finished. Worker (ID=17976) starting. Worker 31580 finished. Worker (ID=31580) starting. Worker 34500 finished. Worker (ID=34500) starting. Worker 34500 finished. Worker (ID=34500) starting. Worker 32292 finished. Worker (ID=32292) starting. Worker 17976 finished. Worker (ID=17976) starting. Worker 34500 finished. Worker 17976 finished. Worker 31580 finished. Worker 32292 finished. Worker 33804 finished. Worker 31392 finished. Worker 33512 finished. Worker 24072 finished. 
+4
source share
2 answers

If I run std::async using the std::launch::async rule, should I not run every asynchronous task in a new thread?

The specification requires that the asynchronous operation be performed "as in a new thread of execution" (C ++ 11 Β§30.6.8 / 11). The important words here are: as if.

An already existing workflow can be reused if and only if the behavior is the same as if a new thread were created. This means, for example, that variables with the storage class thread_local must be reset between asynchronous operations performed on the same thread.

It is not necessary that the thread identifier be reset, because the thread identifier only uniquely identifies the stream during its operation. If the thread terminates, another thread may be started with the first thread identifier.

Is there any major job theft or something like that?

This is implementation specific. The implementation of the C ++ 11 threading library for Visual C ++ 2012 is built on top of the Concurrency Runtime (ConcRT) , which includes a job scheduler.

+11
source

James is right, with one amendment: Microsoft launches the :: :: async launch incorrectly. This was described in detail by the C ++ concurrency working group

+6
source

All Articles