User Tools

Site Tools


c:c_threads:troubleshooting:over-subscription

This is an old revision of the document!


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; which most of the time coincides with the number of logical cores.

#include <thread>
#include <iostream>
 
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.

c/c_threads/troubleshooting/over-subscription.1614961150.txt.gz · Last modified: 2021/03/05 16:19 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki