Slowing down your CPU

Started by
15 comments, last by Boltimus 21 years, 10 months ago
keep in mind that
while(1)
{
for (int a = 0; a < 99999999; a++);
sleep(1);
}
still isn''t 100% stable. you''d still have bursts of cpu usage for 1 ms at a time. depending what you are doing, 1ms might be alot of time. multiple threads at high priority will help in this case.
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
Advertisement
You know that Sleep is not accurate to the millisecond, right ? Depending on the O.S., i''ve seen the minimum time vary from 8 ms to 40 ms.

Y.
Well, I tried the one string approach. I set it''s class to the highest I could, and controlled a global variable via WM_KEYDOWN which in turn controlled the sleepiness of the thread and for some reason it still doesn''t work. Now here''s the wierd thing. If I place a Sleep() command in my main program, I actually get better control over the CPU usage then if I had placed it in the thread!?? If I don''t place it in the main program, and I just have the program create a simple thread (what I mean by simple is that it only looks for a global variable to change from a 0 to 1 so that it can terminate) then my CPU usage goes right away to 100%! ack!! I used sysmon to monitor the number of threads created by CPUKILLER and it seems to only create 2 threadds, however, I''m having little success slowing down the CPU with sleep, etc.... Is there another way..perhaps memory resources? Every where I read, it says that the most limiting factor in the early days of multitasking (and for the most part multi-threading) was memory and the ability of the OS to move it around. If I hog the memory resources from windows (via a HUGE NEW command, or whatever) do you think that might be the ticket?

Thanks!!

~Bolt
~Bolt"All men dream: but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dreams with open eyes, to make it possible." This I did...
Since its open source, you could modify bochs (a multiplatform x86 emulator) to do a wait between instructions. This would likely give you near 100% control of the cpu speed (though you would be limited in what maximum speed you could set)
Boltimus, sleep doesn''t slow the CPU down. It speeds it up. The Sleep call really tells the OS to run some other thread for the amount of time that you''re sleeping.

If you have something like

while(1) {     for(int i=0; i < 90000; i++)           sleep(n);} 


The ''CPU speed'' of your other processess will get slower as n goes down.

Is that what you were doing? Your posting seemed to indicate that you thought sleep would slow the CPU by itself.
as i said before a single realtime thread should be good enough. though the debugger idea is spiffy as well.

btw, you cant just create a worker thread which you set to realtime and expect it to work properly. you must have your main process thread as a realtime thread. worker threads are subjected to the priority of the process thread, and a such dont get a "true" priority as you set it. it has to do with how windows handles time slices.

though in any case, a process at time critical (realtime) will actually supercede the OS. i fail to see how it gives less control.

you may consider using a multimedia timer with a callback that excutes every x milliseconds (your callback better only take x milliseconds to execute). then do some busy work (ie all the sqrt() function or something, DONT call sleep() it only affects the giving up of your timeslice).

unfortunatly unless you know how to write asm code to time a pc, you wont be able to come up wont be able to come up with a decent busy loop thats accurate enough for what you want.
quote:Original post by Anonymous Poster

while(1) {     for(int i=0; i < 90000; i++)           sleep(n);}  


Looks like you forgot the ; at the end of the for-loop.
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)

This topic is closed to new replies.

Advertisement