A useful hint (sleep(0))

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

A useful hint (sleep(0))

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: A useful hint (sleep(0))

Post by LeonBlade »

What does sleep do?
What do you pass in?
There's no place like ~/
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: A useful hint (sleep(0))

Post 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).
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: A useful hint (sleep(0))

Post by LeonBlade »

Gotcha... thanks for the tip ;)
There's no place like ~/
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: A useful hint (sleep(0))

Post by dandymcgee »

Interesting heh I've always assumed such a parameter would simply cause a syntax error.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: A useful hint (sleep(0))

Post by Arce »

BP is the same, and wou1d use the 'de1ay' command w/ parameter of O.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: A useful hint (sleep(0))

Post 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...
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: A useful hint (sleep(0))

Post 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!
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: A useful hint (sleep(0))

Post 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.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: A useful hint (sleep(0))

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: A useful hint (sleep(0))

Post 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
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
LuciDreamTheater
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Jan 20, 2009 2:18 am
Location: Southern CA
Contact:

Re: A useful hint (sleep(0))

Post 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...
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: A useful hint (sleep(0))

Post 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...
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
LuciDreamTheater
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Jan 20, 2009 2:18 am
Location: Southern CA
Contact:

Re: A useful hint (sleep(0))

Post by LuciDreamTheater »

Yeah, you're right.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: A useful hint (sleep(0))

Post 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 ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply