PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Waiting without Sleep() possible?

I was wondernig if it was possible to wait without using Sleep(). I have a previous post that discussed FPS limiting and Sleep() but I never asked the question directly.

The thing I'd like to be able to do is to wait for 1/2 a millisec at a time to get better timing in a critical portion of my software. I have use of this for my game but also for a non-gaming project.

Thanks!

--Eric
Pejo Software - Per · 08 Mar 2004, 13:06 UTC
I think your only option is to do busy waiting and use a performance counter.
Say you determine the precision of Sleep is good to 20 MS. Then you could do something like this:

Code:

void PreciseSleep(nMS) {
int nStartTick=GetTickCount();
if (nMS > 20)
Sleep(nMS-20);

nMS+=nStartTick;
while (GetTickCount()<nMS);
}


Of course it would need some tweaking, but you get the general idea. Sleep away the bulk of the idle time, and use an accurate timer for the small remainder. You could use QueryPerformanceCounter instead of GetTickCount for better accuracy.

Dan East
Pejo Software - Per · 08 Mar 2004, 15:38 UTC
In Windows CE >= 3 the accuracy of Sleep should be 1-2 ms. On a PC it is not that good.

I did some tests on a Cassiopeia E-115 Pocket PC and on that device Sleep(1) would mean sleeping in 20 ms. This is the behaviour that devices < Windows CE 3 would have, but all modern devices will probably have good Sleep accuracy.
Socket sleep realization:
Code:

#include <winsock.h>

void WceSleep(DWORD dwMilliseconds)
{
struct timeval tv;

tv.tv_sec = 0;
tv.tv_usec = 1000 * dwMilliseconds;
select(0, NULL, NULL, NULL, &tv);
memset(&tv, 0, sizeof(tv));
}
Wow! That is awesome, I'll go and try it right away!

I had no idea you could use select() for idle waiting!

--Eric
Using socket select command to realize Sleep has some unwanted influence to power monitoring and clock counter registers.
And using GetTickCount is more correct.
I'll keep that in mind.

So far it seems to be doing a good job.

--Eric
Using select to sleep is how the text-based MUDs do it too. Just look at any Merc, Diku or EnvyMud source code.

:)