Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4042018
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:57:12+00:00 2026-05-20T12:57:12+00:00

My understanding of the Sleep function is that it follows at least semantics i.e.

  • 0

My understanding of the Sleep function is that it follows “at least semantics” i.e. sleep(5) will guarantee that the thread sleeps for 5 seconds, but it may remain blocked for more than 5 seconds depending on other factors. Is there a way to sleep for exactly a specified time period (without busy waiting).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:57:12+00:00Added an answer on May 20, 2026 at 12:57 pm

    As others have said, you really need to use a real-time OS to try and achieve this. Precise software timing is quite tricky.

    However… although not perfect, you can get a LOT better results than “normal” by simply boosting the priority of the process that needs better timing. In Windows you can achieve this with the SetPriorityClass function. If you set the priority to the highest level (REALTIME_PRIORITY_CLASS: 0x00000100) you’ll get much better timing results. Again – this will not be perfect like you are asking for, though.

    This is also likely possible on other platforms than Windows, but I’ve never had reason to do it so haven’t tested it.

    EDIT: As per the comment by Andy T, if your app is multi-threaded you also need to watch out for the priority assigned to the threads. For Windows this is documented here.


    Some background…

    A while back I used SetPriorityClass to boost the priority on an application where I was doing real-time analysis of high-speed video and I could NOT miss a frame. Frames were arriving to the pc at a very regular (driven by external framegrabber HW) frequency of 300 frames per second (fps), which fired a HW interrupt on every frame which I then serviced. Since timing was very important, I collected a lot of stats on the interrupt timing (using QueryPerformanceCounter stuff) to see how bad the situation really was, and was appalled at the resulting distributions. I don’t have the stats handy, but basically Windows was servicing the interrupt whenever it felt like it when run at normal priority. The histograms were very messy, with the stdev being wider than my ~3ms period. Frequently I would have gigantic gaps of 200 ms or greater in the interrupt servicing (recall that the interrupt fired roughly every 3 ms)!! ie: HW interrupts are FAR from exact! You’re stuck with what the OS decides to do for you.

    However – when I discovered the REALTIME_PRIORITY_CLASS setting and benchmarked with that priority, it was significantly better and the service interval distribution was extremely tight. I could run 10 minutes of 300 fps and not miss a single frame. Measured interrupt servicing periods were pretty much exactly 1/300 s with a tight distribution.

    Also – try and minimize the other things the OS is doing to help improve the odds of your timing working better in the app where it matters. eg: no background video transcoding or disk de-fragging or anything while your trying to get precision timing with other code!!

    In summary:

    1. If you really need this, go with a real time OS
    2. If you can’t use a real-time OS (impossible or impractical), boosting your process priority will likely improve your timing by a lot, as it did for me
    3. HW interrupts won’t do it… the OS still needs to decide to service them!
    4. Make sure that you don’t have a lot of other processes running that are competing for OS attention
    5. If timing is really important to you, do some testing. Although getting code to run exactly when you want it to is not very easy, measuring this deviation is quite easy. The high performance counters in PCs (what you get with QueryPerformanceCounter) are extremely good.

    Since it may be helpful (although a bit off topic), here’s a small class I wrote a long time ago for using the high performance counters on a Windows machine. It may be useful for your testing:

    CHiResTimer.h

    #pragma once
    #include "stdafx.h"
    #include <windows.h>
    
    class CHiResTimer
    {
    private:
        LARGE_INTEGER frequency;
        LARGE_INTEGER startCounts;
        double ConvertCountsToSeconds(LONGLONG Counts);
    public:
        CHiResTimer(); // constructor
        void ResetTimer(void);
        double GetElapsedTime_s(void);
    };
    

    CHiResTimer.cpp

    #include "stdafx.h"
    #include "CHiResTimer.h"
    
    double CHiResTimer::ConvertCountsToSeconds(LONGLONG Counts)
    {
        return ((double)Counts / (double)frequency.QuadPart) ;
    }
    
    CHiResTimer::CHiResTimer()
    {
        QueryPerformanceFrequency(&frequency);
        QueryPerformanceCounter(&startCounts); // starts the timer right away
    }
    
    void CHiResTimer::ResetTimer()
    {
        QueryPerformanceCounter(&startCounts); // reset the reference counter
    }
    
    double CHiResTimer::GetElapsedTime_s()
    {
        LARGE_INTEGER countsNow;
        QueryPerformanceCounter(&countsNow);
        return ConvertCountsToSeconds(countsNow.QuadPart - startCounts.QuadPart);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To my understanding a Thread.Sleep(0) force a context switch on the OS. I wanted
My understanding is that Thread.Abort should raise a ThreadAbortException on a blocked thread, however
Understanding HTTP/TCP protocol is a bonus point for a web developer. But does it
Understanding that I should probably just dig into the source to come up with
My understanding of Hibernate is that as objects are loaded from the DB they
My understanding is that wxWidgets is for a number of programming languages (C++, Python,
My understanding of the MVC is as follows (incase it's horribly wrong, I am
My understanding is that C/C++ produces native code to run on a particular machine
I'm trying to clarify my understanding of semantics in the Clips expert system, so
I'm having trouble understanding the correct variable in the next_block(the_file) function. The program won't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.