6

I am using std::threads and in my setup my other threads (variable amount, currently set to 10) are using so much capacity that my used cpu in task manager goes up to 100% for the application. That makes my main thread laggy, which should be real time (I assume that this is the reason for the lag).

I debugged with Intel Amplifier, but there was no other clue why the main thread should lag. My secondary threads where all really busy.

Is it possible to tell a thread how much CPU it can use maximal? How can I make sure that my other threads don't affect the performance of my main thread?

Thread initialisation:

for (int i = 0; i < numberOfThreads; i++)
{
   std::thread* thread = new std::thread(&MyClass::mWorker, this);
   mThreads.push_back(thread);
}

My System: i5-4590 3.3GHz, 8 GB RAM, Windows 8 64 bit, Ogre3D Graphic Engine

Anthea
  • 3,741
  • 5
  • 40
  • 64
  • 3
    You could set the other threads to have lower priority than the main thread: not directly possible in C++11, but you can use the "native" thread id - see [here](http://stackoverflow.com/questions/18884510/portable-way-of-setting-stdthread-priority-in-c11) for discussion/details. – Tony Delroy Aug 27 '15 at 10:42
  • 2
    I agree with Tony D. You may also consider to sleep your lower priority threads conditionally. – gpicchiarelli Aug 27 '15 at 10:46
  • 3
    You could also look for improvements in your current design and implementation. – UmNyobe Aug 27 '15 at 10:46
  • Thanks! Thread priority gave me a boost for 50 FPS! @TonyD, Make it an answer and you get the credits ! – Anthea Aug 27 '15 at 10:50
  • 1
    Can accept Rudolfs' answer - was almost the same time as mine, much the same info (and more - affinity's a good alternative to list), and it's the proper thing to do - post an answer not a comment.... – Tony Delroy Aug 27 '15 at 13:01
  • 2
    I would also notice that 10 threads on 4 cores is a high number. You are talking 2.5 to 1 ratio of threads to core. You might benefit fron lesser number of threads. – SergeyA Aug 27 '15 at 13:05

1 Answers1

8

I don't think there are any C++11 means for doing that, but you can use platform thread scheduling features with the native_handle that you can obtain from an std::thread and then either set the priority of the threads (e.g. - prioritize the main thread) or set thread affinity so that the main thread is bound to an exclusive core that is not used by any of the worker threads.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71