More intelligent std::async decision to make new thread

Periklis Tsirakidis
Tue Mar 14 09:58:00 GMT 2017

Ko Stoffelen <> writes:

> Hi,
> I have a bunch of tasks that I'd like to execute in parallel as fast as
> possible. I'm using std::async for this purpose, without a launch
> policy, which is then std::launch::async | std::launch::deferred by
> default. Now if I understand correctly, the C++ standard leaves it to
> the implementation how it should handle this launch policy. libstdc++
> always first tries to create a new thread and only if that raises an
> exception, the task is deferred.
> In practice, this could imply that I'm running >100 threads on 4 cores
> and the OS is only switching tasks all the time, actually delaying the
> computation. Is it possible to make a more intelligent decision at
> runtime, based on, e.g., the number of cores or the current CPU load?
> Thanks,
> Ko

You could, ask std::thread::hardware_concurrency() before selecting a
policy. For cpu load you need some library to ask for it, i think.

Periklis Tsirakidis              Mayflower GmbH
Mannhardtstraße 6                Tel.: +49 89 24 20 54 1145
D-80538 München                  Fax : +49 89 24 20 54 29


Mayflower GmbH, Standort München
Firmensitz: Mannhardtstraße 6, 80538 München
Registergericht: Amtsgericht München, HRB 142039
Geschäftsführer: Björn Schotte, Albrecht Günther, Johann-Peter Hartmann

More information about the Libstdc++ mailing list