Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » yield_timeslice(), rest(), Sleep() - what to use?

This thread is locked; no one can reply to it. rss feed Print
yield_timeslice(), rest(), Sleep() - what to use?
Daniel Schlyder
Member #257
April 2000
avatar

For some reason I can't get this through to the Allegro mailing list, so I'm posting it here...

I'm reading these mails about yield_timeslice() and my head is spinning...

Could somebody please explain what I can/should put in the else {} below (if anything?). (It's a loop for a crude menu in a windowed game.) I guess what I have been using doesn't make sense?

Thanks in advance.

1while (!exitGame)
2{
3 if (keypressed())
4 {
5 ...
6 }
7 else
8 {
9 // don't use 100% CPU time when waiting for input
10#ifdef _WIN32
11 rest(1); // or Sleep(1) ?
12#else
13 yield_timeslice();
14#endif
15 }
16}

Goodbytes
Member #448
June 2000
avatar

If you want your loop to respond quickly to input, then definitely don't use rest() - those types of delays have a resolution of about 55 milliseconds, so even if you think you're only pausing for one millisecond, you're actually doing it for about one 18th of a second. Also, the delay might cause a buildup in the key buffer. yield_timeslice() is a better choice if you want to play nice with the OS, but I don't think it's necessary - you won't be using a lot of CPU time anyway.

At least, I don't think so. I'm not 100% sure on what I said about yield_timeslice().

Hope this helps a bit.


--
~Goodbytes

Blade Nick
Member #1,597
October 2001
avatar

First off ain't Sleep fir qbasic and why on earth do you wan't to try to slow down the game when pressing a key as this would make an upeven speed truought asdfkj asd F)ÿÄ3§Ä 62 87&%&^$$ fd87 UI87....... Heh something happend anyhough this would give and uneven speed which would be anoying to people unless your idea is to use this stange abomination to somehow takeover the world then go for it but otherwize you are just going so kill ureselfe or someon else wiht a starnge code.. 123468 4518+bAAJ1#ç54354
85)34145+21o212532825_064345 0640ª¬ŽŽÄ66ÄÄ9W_îA3§..... Man my keyboard buffer is stu_ÄÄA_4 do¤B heksdf6
NOOO BULE SCREEN OF DOOM AHHHHHH

Bad Website Of Mine
We, will survive.

Daniel Schlyder
Member #257
April 2000
avatar

I didn't know rest() took that long, but it does seem to respond quickly enough for my needs. As I said, this is not for in-game input, but for a crude menu that goes something like F1... play, F2.. configure, ESC.. exit. I put in the rest() cause when running the game in Windows XP the System Task Manager claims my program/process uses 100% CPU time when using yield_timeslice() or nothing. I guess it's this 100% value that confuses me. I can play mp3s in the background without problems, so it can't really be 100%.

Blade Nick: Sleep() is part of win32 API. yield_timeslice() == Sleep(0) in Windows.

gnolam
Member #2,030
March 2002
avatar

Ok, let me get this straight:

yield_timeslice() == Sleep(0)
Sleep() ==(more or less) rest()

So what the hell does it do then? Resting 0 ms doesn't make any sense to me (and I don't understand what the docs say about it either - all it says is that it "gives up the rest of the current scheduler timeslice"). Anyone care to enlighten me?

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Daniel Schlyder
Member #257
April 2000
avatar

Hehe, I see I'm not the only one who's a bit confused. :) I think rest() uses Allegro's timer code and Sleep() is Win32 function that, err, yields timeslices. :) Sleep(1) seems to have the same effect as rest(1) in my program, but I don't think functionality is equal.

Goodbytes: I re-read your post. I don't fully get timeslices and processes CPU usage, but I think calling yield_timeslice() on all platforms is probably what I should do in my game's menu. Thanks for your help!

Go to: