Loops?

Started by
4 comments, last by di9toml 21 years, 6 months ago
Hi, I''m wondering if anyone has a solution for the following problem: Under Windows operating system we all (?) use tight loops to increase the number of frames per seconds. This will make sure that we have all the available CPU power we can get. If I for some reason want a frame rate about 50, I''ll have to halt my process for some time by waiting in a while-loop. How can I release the CPU under this time and allow it to run other processes or threads? I''ve tried to use some sort of thread-waiting-sleep-event solution but because of Windows it will only get me low fps (32 or so but no pressure on the CPU).
Advertisement
This is problematic, and not sure there is any good solution. But, I think there is a function that just gives up your time-slice (no sleeping/waiting) and acts like your thread's time was up. I think it should only be around a few ms before you get the CPU again. You could make a clever (wait) loop, that will remember how long it took for you to get back control, and only give back the CPU if it's more than that time left to wait. You could also mess with raising the priority of the process.

EDIT: I just threw together something to show what sort of loop I meant. What the realm name of "GiveBackControlToWindows()" is, I don't remember.
dwNextFrameTick = dwLastFrameTick + dwFrameTickCount;dwTicksToGetBack = 0;while((dwCurrentTick = GetTickCount()) < dwNextFrameTick) {    if(dwTicksToGetBack < (dwNextFrameTick - dwCurrentTick))        GiveBackControlToWindows();    dwTicksToGetBack = GetTickCount() - dwCurrentTick;} 


[edited by - CWizard on October 19, 2002 7:29:50 AM]
The standard Win32 API timer functions only fires a message once every 18th of a second.

You probably need to play with the kernel-level timer. Check out:

http://www.bitbanksoftware.com/Programmers/code4.html

HTH

--
Sean Timarco Baggaley




[edited by - stimarco on October 19, 2002 7:45:33 AM]
Sean Timarco Baggaley (Est. 1971.)Warning: May contain bollocks.
CWizard, the name of the function you did not remember is Sleep The call
Sleep(0)
"causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run." (MSDN Library)
quote:Original post by EliasAE
CWizard, the name of the function you did not remember is Sleep The call
Sleep(0)
"causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run." (MSDN Library)
Hmm, probably it was that, although I thought I remember it having a more cryptic name. But, it''s probably Sleep(0), which should allow you to do what you want, if you do it clever.
I thought I remembered something different too. I just can''t seem to find it. OTOH, i''ve programmed in so many different languages, environments, and IDEs, I may be thinking of one of those.
Aaargh! Everyone save your workspaces before your program locks up your computer!

This topic is closed to new replies.

Advertisement