Solved

Timing in Visual C++ 5.0

Posted on 1998-07-17
5
506 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 149
Arduino EDI - Programming language 3 91
Unable to start eclipse ? 17 141
Autosar OS Multicore Share Resources confusion ? 2 35
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

813 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

16 Experts available now in Live!

Get 1:1 Help Now