====== C - C++ Threads - Troubleshooting - Over-subscription ======
Over-subscription is a situation where more threads are vying for runtime than the underlying hardware can support.
One of the biggest cost associated with multiple threads is that of context-switches that happens when the processor switches threads.
Ideally, the you'd not want to create more threads than the hardware can support.
----
===== Avoid Over-subscription when working with multiple threads =====
Obtain the number of threads that can be run in parallel from an application.
Often this might coincide with the number of logical cores, but not always.
#include
#include
int main(int argc, char **argv)
{
unsigned int max_threads = std::thread::hardware_concurrency();
std::cout << "MAX Threads=" << max_threads << std::endl;
return 0;
}
returns:
MAX Threads=24
**NOTE:** This means that no more than 24 threads should be forked.