Skip to main content
gamedev.net
🔒 Locked

Making the computer wait

Started by alex997 12 replies 750+ views
Original Post
alex997
alex997
Hi guys, Not an OpenGL question as such, but I''ve always found this forum so informative and helpful in the past... Basically I need to make a program wait for a certain amount of time, (this time could be < 1 second, or up to 10 minutes) without keeping the processor usage at 99%+ (so continually checking tick count in a loop is not an option). Basically a "resume program running in x seconds" command. To give you a little extra information... After a certain time (perhaps even every second/10 secs/minute) the program will either: a) Perform the next instruction in a pre-stored list b) check to see the existence of a file. This file will contain the next instructions and will have been saved by a different computer on a shared network drive. The program will probably just be a console app, but will be one of 2 programs running on that computer and the other prog is quite resource-hungry, hence the need for this prog to not hog the processor... Any ideas? Hopefully there''s just a "wait" command or something!...
lemurion
lemurion
// will sleep 10 seconds
sleep(10);

hope that helps
Matt
Matt
alex997
alex997
Cheers buddy... That''s just what I was after. I searched for "wait", "pause" and anything else I could think of, but didn''t come up with sleep. Cheers again!
sphinx23
sphinx23
The function 'Sleep' as it is in the Windows API will actually take its argument in Milliseconds.

So the previous example would be:

// Sleep 10 seconds
Sleep( 10000 );


IIRC theres a similar function in DOS, that will take its arguments as seconds (sleep(sec)), and one that take milliseconds as an argument (delay(msec)).

[edited by - sphinx23 on March 3, 2004 9:02:26 AM]
AndyL
AndyL
For unix/linux/etc you''ll want the "sleep(int)" from unistd.h.

I''m virtually positive that takes it''s argument in whole seconds.

It returns an int so you can tell if your program has slept the whole time. If the return value is 0 it''s slept the whole period, if it''s greater than 0 then the value should be the remaining period you wanted to sleep. (It may be revived early if the program gets any signals from the OS.)

I don''t know but I would assume the windows version does something similar.

(Keep in mind that these aren''t guaranteed to be precise. If you need precision timing you''re going to want some sort of real timing code as well. )
Palidine
Palidine
sleep means "sleep for around this amount of time". it can sleep for more, it can sleep for less (though i think it usually sleeps for that amount or longer). so DON'T use this as a method of precision waiting.

if you want precision waiting have some kind of timer loop that loops until the right amount of time has passed. or make whatever you're coding event driven so that it doesn't do anything until it receives the appropriate event (if that's applicable to your situation).

[EDIT: ignore the loop part, as i just read your post cerefully and you're not able to use that ]

-me

[edited by - Palidine on March 3, 2004 2:41:10 PM]
alex997
alex997
That''s great guys. The timing doesn''t need to be precise so Sleep(n) should do fine. Cheers again.
dawidjoubert
dawidjoubert
As far as i know in windows task manager it has a program running called idle procces 93% (93% reading on the cpu) doesnt that mean that whether or not ur actually doing something it is using 100% of the cpu(unless ofcoarse in suspend mode)

Just for intrest sake!! :-)
----------------------------http://djoubert.co.uk
AndyL
AndyL
Doesn''t the idle thread sleep the proccessor for some cycles? (To reduce energy input and heat output?)
Prototype
Prototype
Hehe no, the only thing a computer cannot do is sit still and wait Even the NOP (no operation) instruction takes 4 clockcycles if I''m correct.
Monder
Monder
What about the hlt instruction? This puts the processor into some sort of standby mode which it goes out of when it gets an interrupt.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.