Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BillyBillyBilly

A sleep function that works

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
You need to be a bit more specific about this,
e.g. which OS are you coding for?


// ANSI C - accuracy depending on OS


#include <time.h>

void sleep(int ms) {

clock_t delay = (((clock_t)ms) * CLOCKS_PER_SEC) / 1000;
clock_t start = clock();

while ((clock() - start) < delay);
}



[edit]
Wrong header...
[/edit]


[edited by - darookie on November 10, 2003 2:52:55 AM]

Share this post


Link to post
Share on other sites
It really depends on what exactly you want to do. If you just want the program to accurately pause for a specific amount of time, you can write a function using QueryPerformanceCounter that loops until the specified time has elapsed. Note that by using this method, you''ll be hogging much more CPU time than you would by using the Sleep method (if that matters to you).

Share this post


Link to post
Share on other sites

namespace timer {
int cTime, lTime;

void refresh()
{
lTime=cTime;
cTime=timeGetTime();
}

bool get(int delay)
{
if ((cTime%delay) < (lTime%delay))
return true;
else
return false;
}
}


and to use it its basically


timer::refresh();
if (timer::get(1000))
//do whatever

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ansi''s clock function won''t work since it doesn''t return the system time but rather the amount of time spent in a specific process.
anyway there''s no accurate way to sleep, except perhaps on rtos''s or singletasking systems.

Share this post


Link to post
Share on other sites
One thing you can do is sleep() for a smaller time than desired and then busy wait for the remaining time.


void MySleep(int sleepAmount)
{
Timer t;
t.start();

Sleep( max(0, sleepAmount - SLEEP_RESOLUTION) );

while (t.getTime() < sleepAmount);
}


The resolution of sleep() is about 10 ms for a normal priority thread on WinNT 4/5.

Edit: damn trailing ;

[edited by - jermz on November 10, 2003 12:44:08 PM]

Share this post


Link to post
Share on other sites
I''m programming it for linux, mac, windows.

Right now I just query performance counter, the only problem is that it hogs a bunch of CPU. The Sleep() method would be perfect on windows, except when I say Sleep(1) it doesn''t sleep for a millisecond, it sleeps for 34... it''s bizarre.

I''ve tested it a lot, and I can''t figure out why sleep doesn''t work for me...

Share this post


Link to post
Share on other sites
It is not bizarre, it''s the normal operation of Sleep() for a non-realtime OS. Whenever you call Sleep(X), you are telling the OS you no longer need execution time and would LIKE control back in X ms. The OS will give you control back when it gets the chance, after it takes care of processing all the other processes/threads on the system.

Now 34 ms does seem like a long time to wait for a Sleep(1) call. You could try Sleep(0) which relinquishes the rest of your time-slice but tries to get control back on the next one. Or you could increment the priority of whatever your calling thread is so that normal priority ones don''t interfere as much.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!