Page 1 of 1

A useful hint (sleep(0))

Posted: Thu Jan 29, 2009 2:32 pm
by MarauderIIC
If you're developing something that needs to continuously loop but also doesn't need a framerate limiter (ie, a server) and you don't want it to eat all your CPU, try implementing your heartbeat like this:

Code: Select all

while (run) {
    doGameLogic();
    sendOutput();
#ifdef __WIN32
    Sleep(0); //windows
#else
    sleep(0); //UNIX
#endif
}
Of course you only need a particular #ifdef block for your OS (unless you're developing multiplatform). Sleep(0) essentially tells the OS that your program can be interrupted (Sleep(milliseconds) pauses execution for milliseconds' worth of time). I had a server at one point (MrAdventure) that was eating 99-100% CPU without this, but went down to something reasonable (I think like 20-40%?) after doing this.

If you're using SDL, I think SDL_Delay() is multiplatform, but I'm not sure if it works in the same fashion.

Re: A useful hint (sleep(0))

Posted: Thu Jan 29, 2009 3:39 pm
by LeonBlade
What does sleep do?
What do you pass in?

Re: A useful hint (sleep(0))

Posted: Thu Jan 29, 2009 3:45 pm
by M_D_K
MarauderIIC wrote: Sleep(0) essentially tells the OS that your program can be interrupted.
Thats what it does. It stops program execution for the amount of time you tell it in milliseconds(5000 = 5 seconds).

Re: A useful hint (sleep(0))

Posted: Thu Jan 29, 2009 3:47 pm
by LeonBlade
Gotcha... thanks for the tip ;)

Re: A useful hint (sleep(0))

Posted: Thu Jan 29, 2009 6:49 pm
by dandymcgee
Interesting heh I've always assumed such a parameter would simply cause a syntax error.

Re: A useful hint (sleep(0))

Posted: Sat Jan 31, 2009 9:05 am
by Arce
BP is the same, and wou1d use the 'de1ay' command w/ parameter of O.

Re: A useful hint (sleep(0))

Posted: Sat Jan 31, 2009 1:26 pm
by eatcomics
Arce wrote:BP is the same, and wou1d use the 'de1ay' command w/ parameter of O.
Arce, maybe you need an ipod touch, you wouldn't be missing any buttons...

Re: A useful hint (sleep(0))

Posted: Mon Feb 02, 2009 2:15 pm
by dandymcgee
Arce wrote:My L, J, and 0 keys are broken.
:lol: I was wondering why the quality of your posts has been slowly decreasing lately. Get a new keyboard!

Re: A useful hint (sleep(0))

Posted: Mon Feb 02, 2009 3:35 pm
by trufun202
I have to do the same thing in .NET with a Windows Service. If I have a delegate that's processing a request, I first do a Thead.Sleep(0) to avoid hangups.

Re: A useful hint (sleep(0))

Posted: Mon Feb 02, 2009 4:15 pm
by MarauderIIC
dandymcgee wrote:
Arce wrote:My L, J, and 0 keys are broken.
:lol: I was wondering why the quality of your posts has been slowly decreasing lately. Get a new keyboard!
I wrote that. I exercised my administrative privilege.
Glad to know it's used in industry, trufun, thanks :D

Re: A useful hint (sleep(0))

Posted: Tue Feb 03, 2009 11:32 am
by Arce
trufun202 wrote:I have to do the same thing in .NET with a Windows Service. If I have a delegate that's processing a request, I first do a Thead.Sleep(0) to avoid hangups.
Huh, wow...I thought I was just being a cheap cunt? Didn't know that was actually what you're 'supposed' to do. xD

Re: A useful hint (sleep(0))

Posted: Tue Feb 03, 2009 12:12 pm
by LuciDreamTheater
dandymcgee wrote:Interesting heh I've always assumed such a parameter would simply cause a syntax error.
It's a function that takes an integer as its only parameter, so it wouldn't cause a syntax error. This is true for all functions in C++ that take an integer, unless I'm mistaking.

I suppose that if you forced the compiler to treat warnings as errors, then there might be an signed/unsigned mismatch. Who knows...

Re: A useful hint (sleep(0))

Posted: Tue Feb 03, 2009 1:42 pm
by Ginto8
anothrguitarist wrote:
dandymcgee wrote:I suppose that if you forced the compiler to treat warnings as errors, then there might be an signed/unsigned mismatch. Who knows...
:| It shouldn't... 0 isn't negative, and the range of a normal unsigned int is from 0 to 4,294,967,295, not 1 to 4,294,967,295...

Re: A useful hint (sleep(0))

Posted: Tue Feb 03, 2009 5:01 pm
by LuciDreamTheater
Yeah, you're right.

Re: A useful hint (sleep(0))

Posted: Wed Feb 04, 2009 7:37 pm
by dandymcgee
I suppose what I was referring to is technically a run-time error not a syntax error.. either way I didn't know you could pass zero ;)