Article View: borland.public.cppbuilder.nativeapi
Article #15805Re: how to sleep 10 milisecond
From: Ted Lyngmo
Date: Fri, 13 Jun 2003 04:31
Date: Fri, 13 Jun 2003 04:31
60 lines
3394 bytes
3394 bytes
Bob Gonder wrote:
> "Boguslaw Fries" <B.Fries@luca-online.de> wrote:
>
>>I need a function, which sleeps my thread for 5 - 10 milisecond but not
>>more!
>>Standad function Sleep( 10 ) - in cfact sleeps my thread fo 30 milisecond
>>
>>Any suggestions?
>
> Three suggestions:
> 1) Get rid of Windows and use a real-time OS.
> 2) Up your priority and only Sleep(5). You'll come back sometime after
> the 5, and with a high enough priority, you stand a chance of comming
> back within 10 (I'd guess).
> 3) Aren't there some high precision timers that would trigger you when
> they go off?
Are you refering to the *WaitableTimer? Yes, it can be set to go off at 100
nanosecond intervals. I have no idea how accurate it really is. I have never
used it.
If you refer to the WinAPI *PerformanceCounter, it won't go off, it just
counts time. Something like this:
DWORD sleeptime=10;
LARGE_INTEGER Before, After, Freq;
if( QueryPerformanceFrequency(&Freq) ) {
CriticalSection
QueryPerformanceCounter(&Before);
Sleep(sleeptime);
QueryPerformanceCounter(&After);
/*
** Diff in seconds:
** (double)(After.QuadPart-Before.QuadPart)/Freq.QuadPart -
** (sleeptime/1000.0)
*/
}
You could boost the thread priority to reduce the impact of context switching
to get a more accurate diff and you could also loop many times to get the
average and min/max diffs. In such a loop, you could also slowly adjust
sleeptime until you get the sleeptime to be used in subsequent Sleep()s to get
the pause duration you need.
The *PerformanceCounter is very accurate. A friend and I used it to measure
the frequency of pulses (from the DSR pin on the serial port connected to a
tone generator - not recommended by any serial port manufacturer :-) and had
an oscilloscope as a reference counter. From what I recall, we got a very
little differance (I don't recall the % but we decided it was negligible) in
the result on the scope and using the *PerformanceCounter on NT4 up to
something like 300000pulses/s. Not a very scientific statement perhaps, but I
wouldn't hesitate using it for similar tasks in the future. :)
Kind regards,
Ted Lyngmo
--
Replace MY_FIRST_NAME in my email address with my first name to send a private
email to me.
Technical Details
Message-ID:
<3EE953A4.4010409@lyncon.se>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3ee8c0df@newsgroups.borland.com> <ejuhevkfhbar1ca95u5jrkg3a4mtbie16j@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,b6af681927951d13 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-06-12 21:44:02 PST Message-ID: <3EE953A4.4010409@lyncon.se> Date: Fri, 13 Jun 2003 06:31:32 +0200 From: Ted Lyngmo <MY_FIRST_NAME@lyncon.se> Organization: Lyncon - Computer & Music consulting User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1 X-Accept-Language: sv,en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: how to sleep 10 milisecond References: <3ee8c0df@newsgroups.borland.com> <ejuhevkfhbar1ca95u5jrkg3a4mtbie16j@4ax.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 217.215.147.98 X-Trace: newsgroups.borland.com 1055478746 217.215.147.98 (12 Jun 2003 21:32:26 -0700) Lines: 61 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail