Using the standard development tools and compilers for the platform[1], does std::async
spawn a new OS thread for each background job or does it use a thread pool or some system based on work stealing task queues?
What is thread-based asynchronous programming The thread-based asynchronous programming approach, also called “ work-stealing ” or “ bulkheading ”, allows one thread pool to hand over a task to another thread pool (let’s call it a work thread pool) and be notified to handle the result when the worker thread pool is done with the task.
Any number of threads can execute the calling class code, but as long as the AsyncClient can complete a given task in less than 300 ms, they will all remain free to do other things while their Future is not completed. This is how asynchronous programming helps us achieve massive system scale.
This thread pool is enhanced with algorithms that determine and adjust the number of threads to run, and provide load balancing to maximise throughput. These features make the Tasks relatively lightweight and handle effectively threads than before.
Using a thread pool reduces the performance penalty by sharing and recycling threads; it exists primarily to reuse threads and to ensure the number of active threads is optimal. The number of threads can be set to lower and upper bounds. The minimum applies to the minimum number of threads the ThreadPool maintains, even on idle.
An application built with the standard toolchain for the platform (Xcode/Clang) does not use a thread pool. The base of the stack of a task launched with std::async
contains std::thread and pthread calls.
On exit, each job calls pthread_exit()
to kill the thread running it.
Xcode 8.3.3 also uses an OS thread per job launched with std::async
when building for iOS (Tested on original iPad Pro 9.7" target).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With