Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Threading maximal CPU for a thread

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

like image 857
Anthea Avatar asked Aug 27 '15 10:08

Anthea


1 Answers

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.

like image 78
Rudolfs Bundulis Avatar answered Sep 17 '22 08:09

Rudolfs Bundulis