The standard idiom seems to be to create n = std::thread::hardware_concurrency()
threads and place them into a thread pool. But the main thread is a thread like any other, hence we might create just n - 1
threads and consider the main thread a part of the thread pool and save some resources. Is there any reason why this should not be done?
A thread pool is a collection of threads which are assigned to perform uniformed tasks. The advantages of using thread pool pattern is that you can define how many threads is allowed to execute simultaneously.
A thread pool is a collection of worker threads that efficiently execute asynchronous callbacks on behalf of the application. The thread pool is primarily used to reduce the number of application threads and provide management of the worker threads.
A thread pool helps mitigate the issue of performance by reducing the number of threads needed and managing their lifecycle. Essentially, threads are kept in the thread pool until they're needed, after which they execute the task and return the pool to be reused later.
If you do computation in the main thread as well, then sure.
But more often than not the idiom that I see is that the main thread dispatches the work into the thread pool and then just waits for the thread pool to finish. If that wait is not done with busy-waiting, but something like a condition_variable
, then it does not occupy a processor core for very long.
It is also common for the main thread to be used to handle operating system signals. Especially in the case of a UI application the main thread needs to be kept responsive, so using it for potentially longer running tasks leads to a bad user experience.
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