zaterdag 16 november 2019

Sleeping a Thread in a realtime game

In my SDL 2 racing game, I use 2 threads and they use approx. 25% of the CPU power. The CPU speed is 3.80 Ghz in this case. The game runs realtime and in every frame the code uses approx. 5 - 7 ms.

I tested using 3 threads and then the code uses approx. 3 - 5 ms in each frame. So, 3 threads is faster. However, the CPU utilization becomes 37%! And that is on my PC. Other slower PC's might get a higher CPU utilization.

So, I thought, maybe I can use a Mutex to let the operating system use the threads in the remaining time for each frame that is not used.
It worked. I used EnterCriticalSection(pCritSec) and LeaveCriticalSection(pCritSec). The CPU utilization goes down to 12%. A profit of 25% CPU utilization!

However, the CPU speed clocks down from 3.8 Ghz to 2.0 Ghz and the code speed for each frame becomes unpredictable: 3 - 13 ms, and that is unacceptable.

Then I did not use the mutex on one thread of the 3. The result was that the CPU runs at 3,80 Ghz but the CPU utilization is 25%. The code speed is 4 - 6 ms per frame. This seems to be the best solution.

Geen opmerkingen:

Een reactie posten