Solved

Timing in Visual C++ 5.0

Posted on 1998-07-17
5
498 Views
Last Modified: 2013-12-14
How can i realize a 0.5ms (or less) timer in a console-application ?
Is it possible to do it without busy waiting
0
Comment
Question by:frefel
5 Comments
 

Author Comment

by:frefel
ID: 1168104
Edited text of question
0
 
LVL 2

Expert Comment

by:VEngineer
ID: 1168105

Most timer functions I've seen work in increments of integral milliseconds, not fractional.  Few timers are accurate at less than 100 ms.
0
 
LVL 4

Accepted Solution

by:
agreen earned 100 total points
ID: 1168106
Windows NT bases all of its timer support off of one system clock interrupt, which by default runs at a 10 millisecond granularity. This is therefore the resolution of standard Windows timers. When, for example, a multimedia application uses the timeBeginPeriod mutlimedia API, which is exported by the Windows NT dynamic link library WINMM.DLL, the call is redirected into the Windows NT kernel-mode function NtSetTimerResolution, which is exported by the native Windows NT library NTDLL.DLL. NtSetTimerResolution and NtQueryTimerResolution are defined as follows. All times are specifified in hundreds of nanoseconds.

NTSTATUS NtSetTimerResolution (
                           IN ULONG RequestedResolution,
                           IN BOOLEAN Set,
                           OUT PULONG ActualResolution
                   );

Parameters

                   RequestedResolution

                   The desired timer resolution. Must be within the legal range of system timer                     values supported by NT. On standard x86 systems this is 1-10 milliseconds.                    Values that are within the acceptable range are rounded to the next highest                    millisecond boundary by the standard x86 HAL. This parameter is ignored if                     the Set parameter is FALSE.

                   Set

                   This is TRUE if a new timer resolution is being requested, and FALSE if the                     application is indicating it no longer needs a previously implemented                    resolution.

                   ActualResolution

                   The timer resolution in effect after the call is returned in this parameter.

                   Comments

                   NtSetTimerResolution returns STATUS_SUCCESS if the resolution                                 requested is within the valid range of timer values. If Set is FALSE, the caller                    must have made a previous call to NtSetTimerResolution or
                   STATUS_TIMER_RESOLUTION_NOT_SET is returned.

NTSTATUS NtQueryTimerResolution (
                 OUT PULONG MinimumResolution,
                 OUT PULONG Maximum Resolution,
                 OUT PULONG ActualResolution
                 );

Parameters

                   MinimumResolution

                   The minimum timer resolution. On standard x86 systems this is 0x2625A,                       which is about 10 milliseconds

                   MaximumResolution

                   The maximum timer resolution. On standard x86 systems this is 0x2710,                    which is about 1 millisecond.

                   ActualResolution

                   This is the current resolution of the system clock.

Under Windows 95 you cannot change the timer resolution !!!
0
 
LVL 1

Expert Comment

by:Rajkumar_R
ID: 1168107
Yes you can realize timers even to the precision of micro seconds using select call !

Sample code :-

struct timeval      stTimeVal;
stTimeVal.tv_sec = 0;      // seconds feild !
stTimeVal.tv_usec = 1000 ; // 1milli second !

select (1,NULL,NULL,NULL,&stTimeVal); // this sleep for that much time !

If this works for you let me know i will repost my question as an answer(After you reject the current one) then you can give me points !
0
 

Expert Comment

by:joshual
ID: 1168108
You may like to read "Encapsulating Windows Timers in MFC (jan.1998)". at http://www.vcdj.com/

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now