Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ async use a thread pool when building for macOS with Xcode?

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?

  1. Xcode, Clang/LLVM
like image 590
ahcox Avatar asked Aug 04 '17 15:08

ahcox


People also ask

What is thread-based asynchronous programming?

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.

How many threads can an asyncclient execute?

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.

What is a thread pool in Linux?

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.

What are the benefits of using a ThreadPool?

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.


1 Answers

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.

Stack trace showing async job running on a dedicated OS thread

On exit, each job calls pthread_exit() to kill the thread running it.

enter image description here

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).

enter image description here

like image 99
ahcox Avatar answered Sep 20 '22 17:09

ahcox