Need accurate timers in Windows AND Linux

Posted on 2006-05-02
Last Modified: 2008-01-16
We're programming a system which needs to timeout on a serial port within 50ms. Originally I was using the variants of GetLocalTime or gettimeofday for Windows or Linux as appropriate, but I have subsequently found out that these are highly innacurate - at least on Windows.

For Windows I can use QueryPerformanceCounter/Frequency. Great. But not on Linux - this is a cross platform application. But let's stick with Windows for the moment.

There are plenty of examples of QueryPerformanceCounter on the net. But I've yet to find one that takes into account wrap around. I'm sure this must happen at some point, even if it is a 64 bit number. So my first question is, is this code correct for taking into account such wrap-arounds:
v1 := QueryPerformanceCounter(...)
// do some work
v2 := QueryPerformanceCounter(...)
if v2 < v1 then begin
  Result := High(Int64) - v1 + v2;
end else begin
  Result := v2 - v1;

Second question: looks okay (subject to the wrap-around amendments) but is this accurate or reliable enough for Linux? 10ms would be fine.


Geoff M.
Question by:gmayo
    LVL 12

    Accepted Solution

    Hello, Geoff,

    Sorry, but I think that you will not be pleased by my answers.  

    Windows is NOT a "real-time" operating system, and so you will find that it is not possible to control your time intervals with absolute reliability.  If your timing contraints are not too rigid you might be able to achieve them most of the time.  But there are no guarantees.  If this is good enough, OK.  Otherwise you will need an operating system that supports real-time operations.  For more information see:

    Re your first question, I seem to recall that more importantly than any "wrap-around" issue, QueryPerformanceCounter suffered from a problem where the "count" would occassionally "jump" resulting in incorrect results.  I think that the jump was always significant, so for my application I was able to determine that the resulting interval was invalid and simply ignore it.  Applying a similar solution in your case would result in occassionally either missing or mis-identiying a timeout.  If this is acceptable, then it should also handle the far less frequent case of "wrap around".  See;EN-US;Q274323

    for details.

    Sorry, but I don't have any knowledge about the Linux timers.

    LVL 2

    Expert Comment

    Hello omegaomega:

    Pc clock ticks only 18 times per second (every 55 milliseconds), so i f you need resolution around 50 ms you can not rely on this clock.

    You should get an internal PCI clock card and program your software to read the clock card time instead of the OS clock.

    Check this one

    You may get lot of cards like this.

    LVL 8

    Author Comment

    Firstly, we HAVE to use Windows and Linux. Another platform is not viable. Having something which works fairly reliably is important but 100% split second accuracy is not essential. However, a resolution of 55ms is not good enough either.

    I wasn't aware of the jumping issue, so I appreciate that link. Quite ironic that one still has to use GetTickCount to determine whether a jump has occurred!

    This internal PCI clock card doesn't give any better accuracy or resolution than a standard PC clock. All it's doing is supplying the right time. That is not important in our application as time will be fed from a central atomic time source. The important factor is the relative *intervals* between polling times.

    I haven't tried the source code I mentioned on Linux yet, but putting it onto Windows cures my problems - not a single timeout anymore, whereas with GetTickCount and GetTime I was getting numerous.

    Thanks, both of you, for the tips.

    Geoff M.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now