Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sleep API does not give a delay of 1 milli second

12 views
Skip to first unread message

Vijay Anand

unread,
Aug 20, 2002, 3:23:42 AM8/20/02
to
Hi there,
I am writing an application in which i need to introduce a delay
for 1 milli secods between two operations.. (Hardware settling time for
RELAY ON/OFF)..
I tried using Sleep API... But iam getting around 10 milli seconds for
Sleep(1). Worse, if i call that Sleep API inside a for loop... Below is my
code...

for(int count = 0; count < 1000; count++) {
Sleep(1);
}

The above code takes around 10 seconds in windows 2000 and more than 6
seconds in Windows 9X....

So i decided to use some other mechanism.... I wrote a device driver which
exposes a call to KeStallExecutionProcessor() (inside a for loop) and now
using that for a delay resolution of 1 milli seconds... Is there anyother
API which provides resolution in terms of 1 millisecond...
Any comments will be helpful....

Thanks in Advance...

Regards,
Vijay


puzzino

unread,
Aug 20, 2002, 4:28:53 AM8/20/02
to
Have a look at the foolowing articles:
http://www.microsoft.com/hwdev/platform/proc/mm-timer.asp
http://www.naughter.com/cputicker.html

Hope they help.
Happy programming!
Marco

"Vijay Anand" <vi...@qmaxtest.com> wrote in message
news:enXGOpBSCHA.3936@tkmsftngp12...

Vijay Anand

unread,
Aug 20, 2002, 4:53:27 AM8/20/02
to
Thanks...

Regards,
Vijay

"puzzino" <marc...@eri.ericsson.se> wrote in message
news:ajsu9t$1bv$1...@newstoo.ericsson.se...

Erik

unread,
Aug 20, 2002, 8:58:57 PM8/20/02
to
1 Msec? The timer does not have that kind of granularity.

Try a few debug GetTick and see the granularity. If I
remember right I never got better than 30msec granularity.

Erik

>.
>

Gary Chanson

unread,
Aug 21, 2002, 12:10:11 AM8/21/02
to

"Erik" <ean...@pobox.com> wrote in message
news:550601c248ad$edc26f80$35ef2ecf@TKMSFTNGXA11...

> 1 Msec? The timer does not have that kind of granularity.
>
> Try a few debug GetTick and see the granularity. If I
> remember right I never got better than 30msec granularity.

You should get 10ms on most platforms.

--

-Gary Chanson (MVP for Windows SDK)
-Software Consultant (Embedded systems and Real Time Controls)
-gcha...@mvps.org

-Abolish public schools


Arnaud Boussus

unread,
Aug 21, 2002, 8:09:42 AM8/21/02
to
Hello,

Have you tried Multimedia Timers :
Multimedia timer services allow applications to schedule timer events with
the greatest resolution (or accuracy) possible for the hardware platform.
These multimedia timer services allow you to schedule timer events at a
higher resolution than other timer services.

These timer services are useful for applications that demand high-resolution
timing. For example, a MIDI sequencer requires a high-resolution timer
because it must maintain the pace of MIDI events within a resolution of 1
millisecond.

Applications that do not use high-resolution timing should use the SetTimer
function instead of multimedia timer services. The timer services provided
by SetTimer post WM_TIMER messages to a message queue, while the multimedia
timer services call a callback function. Applications that want a waitable
timer should use the CreateWaitableTimer function.

ab

"Vijay Anand" <vi...@qmaxtest.com> a écrit dans le message de news:
enXGOpBSCHA.3936@tkmsftngp12...

Axel Mammes

unread,
Aug 21, 2002, 10:03:56 AM8/21/02
to
Check out my replacement for Sleep( ) using multimedia timers. I've been
using this for about three years now with great results.
Doing MySleep(1) gets you a 950usec-1050usec delay about 99% of the times,
if your process doesn't get preempted.

Axel

void PASCAL SleepWakeupHandler( DWORD id, DWORD msg, DWORD EventHandle,
DWORD dw1, DWORD dw2)
{
SetEvent( (HANDLE)EventHandle );
UNREFERENCED_PARAMETER( msg );
UNREFERENCED_PARAMETER( dw1 );
UNREFERENCED_PARAMETER( dw2 );
UNREFERENCED_PARAMETER( id );
}


void MySleep( DWORD msTime )
{
HANDLE TimeIsUp = CreateEvent( NULL, TRUE, FALSE, NULL) ;
UINT TimerEvent;
static InitDone = false;
static TIMECAPS TimerCaps;

if( !InitDone )
{
timeGetDevCaps( &TimerCaps, sizeof( TimerCaps ) );
InitDone = true;
}
if( !msTime )
return;
timeBeginPeriod( TimerCaps.wPeriodMin );
TimerEvent = timeSetEvent( msTime, 0, (LPTIMECALLBACK)SleepWakeupHandler,
(DWORD)TimeIsUp, TIME_ONESHOT);
if( !TimerEvent )
{
// ran out of available timers, so just Sleep().
Sleep( msTime );
}
else
{
WaitForSingleObject( TimeIsUp, msTime );
timeKillEvent( TimerEvent );
}
timeEndPeriod( TimerCaps.wPeriodMin );
CloseHandle( TimeIsUp );

}


"Vijay Anand" <vi...@qmaxtest.com> wrote in message
news:enXGOpBSCHA.3936@tkmsftngp12...

Vijay Anand

unread,
Aug 21, 2002, 10:07:27 AM8/21/02
to
Thanks for all the responses..
I have tried Multimedia timers also...but unable to get a resolution of one
millisecond...If possible i need to implement a mechanism that will block my
thread for one millisecond (1 to 1.5 milliseconds). Is it possible to
achieve this under Windows? My application has to run on all NT platforms...
Thanks in advance...

Regards,
Vijay

"Arnaud Boussus" <abou...@graphnet.fr> wrote in message
news:ONHY5tQSCHA.4260@tkmsftngp09...

Gary G. Little

unread,
Aug 21, 2002, 2:16:36 PM8/21/02
to
This question pops up regulary for NT through XP. NT and above implements
the "quantum" for all processes, and typically a quantum is 10 ms. In
Windows NT and above, you simply cannot "sleep" reliably less than 10 ms.
The MM timers will give you 1 ms "most" of the time, but the operative word
here is "reliably".

1 ms sounds like an awfully long settling time for hardware. Registers
usually settle out after a few microseconds, and that kind of stall can best
be done in the driver using KeStallExectionProcessor for no more than 50 us.
Trying to do this at the application level is in all practicaltiy
impossible.

--
Gary G. Little
gli...@broadstor.com
gli...@inland.net


Fred Jackson

unread,
Aug 21, 2002, 4:50:33 PM8/21/02
to
He's talking about RELAYS settling. 1 ms is about right.

"Gary G. Little" <gli...@broadstor.com> wrote in message
news:ak0le5$p44$1...@nntp2-cm.news.eni.net...


> 1 ms sounds like an awfully long settling time for hardware. Registers
> usually settle out after a few microseconds
>

Gary Chanson

unread,
Aug 22, 2002, 12:00:26 AM8/22/02
to

"Vijay Anand" <vi...@qmaxtest.com> wrote in message
news:uzyUgvRSCHA.2008@tkmsftngp12...

> Thanks for all the responses..
> I have tried Multimedia timers also...but unable to get a resolution of
one
> millisecond...If possible i need to implement a mechanism that will block
my
> thread for one millisecond (1 to 1.5 milliseconds). Is it possible to
> achieve this under Windows? My application has to run on all NT
platforms...
> Thanks in advance...

How about upping the priority of your thread and spinning in a loop
until the time elapses?

Vijay Anand

unread,
Aug 22, 2002, 4:41:37 AM8/22/02
to
Thanks Gary. I think blocking the thread will be a lot better than spinning
in a loop. I dont want to increase the load on CPU.

Regards,
Vijay

"Gary Chanson" <gcha...@no.spam.TheWorld.com> wrote in message
news:ak1plj$r5v$6...@pcls4.std.com...>

Vijay Anand

unread,
Aug 22, 2002, 5:12:23 AM8/22/02
to
Thanks Axel, Gary and Fred.
Thanks for your valuable ideas...
Yes i am talking about Hardware Relays and most of them need atleast a milli
second to get settled.
I have used KeStallExecutionProcessor in my device drivers for a few micro
second delays...But now iam writing an user mode application. Looping around
a DeviceIoControl to call that KeStallExecutionProcessor looks like a bad
idea... Seems that i have to settle down with multimedia timers...
Thanks again...


Regards,
Vijay

"Fred Jackson" <fjackson_at_...@nowhere.com> wrote in message
news:e#UlCRVSCHA.1796@tkmsftngp13...

Ziv Caspi

unread,
Aug 22, 2002, 11:16:04 AM8/22/02
to
On Thu, 22 Aug 2002 14:42:23 +0530, "Vijay Anand" <vi...@qmaxtest.com>
wrote:

>Thanks Axel, Gary and Fred.
>Thanks for your valuable ideas...
>Yes i am talking about Hardware Relays and most of them need atleast a milli
>second to get settled.
>I have used KeStallExecutionProcessor in my device drivers for a few micro
>second delays...But now iam writing an user mode application. Looping around
>a DeviceIoControl to call that KeStallExecutionProcessor looks like a bad
>idea... Seems that i have to settle down with multimedia timers...
>Thanks again...
>
>
>Regards,
>Vijay

If memory serves, all you need to do is use the multimedia timers to
increase the resolution of the clock from ~10ms to 1ms (the max rate
NT has APIs for). Then, everything that depends on that timer becomes
more accurate, including the Sleep() calls, GetTickCount(), and the
accuracy of measuring the CPU usage.

(Again, it was quite a long time since I've done that work, so I might
remember wrong.)

HTH, Ziv.

Fred Jackson

unread,
Aug 22, 2002, 1:02:00 PM8/22/02
to
I think the MM timer will work; maybe not optimally, but well
enough to get the job done. Actually it is my understanding
that either multimedia or regular timers/sleeps can be short
by up to one minimum-interval (1 ms for MM timers, 10 ms
"in most common systems *" for regular timers/sleeps), or long
by what is actually an unbounded amount, depending on what
else is going on in the system.

Too long wouldn't normally hurt when actuating relays (it just
makes things run slower than optimum), but too short (zero in
the extreme case) would cause failure.

So, if I'm right, try an MM timer for 2 ms when you need 1 ms
minimum delay.

I've used MM timers in NT, and raised the priority class of my
process and/or the priority of the process' main and/or other
threads when I wanted really stable timing, and have been
fairly impressed by the results. You do have to be careful
when raising priority, because if you tie up the processor
continually for more than brief periods without yielding,
you can easily bring the whole system to its knees.

~~~~~

* E.g., I seem to recall the figure is 15 ms for dual
processor x86 NT systems; not sure if that has been changed
in 2K or XP.


"Vijay Anand" <vi...@qmaxtest.com> wrote in message

news:uvLkPAcSCHA.1640@tkmsftngp11...

Axel Mammes

unread,
Aug 22, 2002, 3:34:20 PM8/22/02
to
I forgot to say that my process is running as HIGH_PRIORITY.
Since WaitForSingleObject puts the thread in wait state, you need your
process and thread to be scheduled back immediately after the MM timer
expires, or else MySleep(1) could delay 50ms or 5000ms, depending on the
CPU愀 workload. The best wayt to test your code is using CPUStress.

Axel

"Fred Jackson" <fjackson_at_...@nowhere.com> wrote in message

news:eBUc$1fSCHA.2556@tkmsftngp09...

Gary Chanson

unread,
Aug 23, 2002, 12:01:10 AM8/23/02
to

"Vijay Anand" <vi...@qmaxtest.com> wrote in message
news:eqpWIebSCHA.1692@tkmsftngp13...

> Thanks Gary. I think blocking the thread will be a lot better than
spinning
> in a loop. I dont want to increase the load on CPU.

That's fine (and I agree) if you can do it, but you'll probably find it
hard to get the short interval you want.

Vijay Anand

unread,
Aug 24, 2002, 3:16:43 AM8/24/02
to
Thanks Axel.
I hear you. I will up my thread priority.

Regards,
Vijay

"Axel Mammes" <mam...@hotmail.com (put s instead of z)> wrote in message
news:esV$pLhSCHA.1900@tkmsftngp09...

Dave Parsons

unread,
Aug 24, 2002, 10:05:51 AM8/24/02
to
On Tue, 20 Aug 2002 07:23:42 UTC, "Vijay Anand" <vi...@qmaxtest.com> wrote:

> Hi there,
> I am writing an application in which i need to introduce a delay
> for 1 milli secods between two operations.. (Hardware settling time for
> RELAY ON/OFF)..
> I tried using Sleep API... But iam getting around 10 milli seconds for
> Sleep(1). Worse, if i call that Sleep API inside a for loop... Below is my

Have a look here:-
www.sysinternals.com/ntw2k/info/timer.shtml

--
Dave

Egbert Nierop (MVP for IIS)

unread,
Aug 31, 2002, 10:56:24 PM8/31/02
to
"Dave Parsons" <dwpa...@t-online.de> wrote in message
news:Ej0w7lFo08Zw-p...@jupiter.dwparsons.dialin.t-online.de...

> On Tue, 20 Aug 2002 07:23:42 UTC, "Vijay Anand" <vi...@qmaxtest.com>
wrote:
>

Vijay, cool stuff. The WINMM.DLL really has the best resolution on NT
available and sysinternals even documents the native DLL export from
NTDLL.DLL. Microsoft does not document this since NTDLL is for internal use
only (sort of)...


Vijay Anand

unread,
Sep 3, 2002, 3:17:24 PM9/3/02
to
Thanks for answering my question Egbert. It seems the multi media timers
also calls those (same) functions exported from
NTDLL.dll...(NtSetTimerResolution) Correct me if iam wrong.

Regards,
Vijay

"Egbert Nierop (MVP for IIS)" <egbert...@nospam.com> wrote in message
news:OsYzLMWUCHA.4092@tkmsftngp11...

0 new messages