So I've written a 3D Software Renderer over the last couple of weeks, and a while ago I added OpenMP support. Just a single "#pragma omp parallel for schedule(dynamic, 100)" before the loop that draws all the triangles. It speeds up the rendering nearly 4-fold (on a quad-core CPU), which is great, but now I don't seem to be able to reduce the CPU usage at all.
I have a sleep() call at the end of the main loop to limit the frame rate. When running on just one core, it works fine, bringing the CPU usage down to about 4% when it's not drawing anything (but is still calculating coordinates of ~9000 vertices).
But when using all 4 cores, even with nothing on-screen, the CPU usage only drops down to 88% or so (down from 99). The frame-rate limiting is still working, it runs at 100fps (would go up to 350 or so otherwise). It just seems like the threads aren't giving up their CPU time outside the parallel region.
Is there a better alternative to get a parallel program to sleep? I've heard sleep() isn't a great way of doing it anyway.
To avoid you having to go through all my code, I've written a very simple program that shows off the problem I'm having. http://pastebin.com/KCmpriZS
If I run that with openMP off, I get about 10% CPU usage without the Sleep(50), and 0% CPU usage with it.
With openMP ON, I get about 75% CPU usage with AND without the Sleep(50).
If I increase it to Sleep(500) then the usages does drop to about 10%, but that's still absurdly high for something that's running only twice a second.
NOTE: that program runs significantly slower with openMP enabled, but that's besides the point, sleeping for 50ms per iteration should reduce the CPU usage to 0, but it doesn't.