When not to use std::async
As we have seen during this chapter, std::async
does not provide direct control over the number of threads used or access to the thread objects themselves. We know now how to limit the number of asynchronous tasks by using counting semaphores, but there might be some applications where this is not the optimal solution and fine-grained control is required.
Also, the automatic management of threads can reduce performance by introducing overhead, especially when many small tasks are launched, leading to excessive context switching and resource contention.
The implementation imposes some limit on the number of concurrent threads that can be used, which can degrade performance or even throw exceptions. As std::async
and the available std::launch
policies are implementation-dependent, the performance is not uniform across different compilers and platforms.
Finally, in this chapter, we didn’t mention how to cancel an asynchronous task started by...