main thread and thread pools

Viewed 303

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?

1 Answers

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.

Related