C++ – Multithreaded Engine Windowed random slowdown


We have a task based multithreaded engine set up where a scheduler passes the task to the threads lock-free queue. The engine is in C++ with DirectX for the rendering and we are using boost::thread for creating the threads. When in windowed mode, it randomly slows down for a second or so and then speeds back up. It seems that it's something that Vista seems to be causing, but we can't figure out how to solve it properly.

One thing that we tried that seemed to help with the random slowdowns was making the thread sleep for a millisecond after each task has been processed, but it causes other problems and isn't really a great solution.

Best Solution

The first thing I would recommend doing is understanding what's causing the slowdown by profiling.

Throwing in random sleeps is rarely a good idea (speaking from experience here, yes I've done this and yes I've fixed this later) and neither is speculating on sources of performance problems particularly in a multi-threaded environment.

Visual Studio 2010 beta1 has a great profiler that is perfect for understanding what's causing the slowdowns if it's within your application, Hazim Shafi's blog walks through how to use it.

You can also look at the xperf tool which is available in the windows performance toolkit (you have to use the platform sdk installer, but you only need to install that node so it's actually pretty fast).