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

  • Home
  • SEARCH
  • 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 8685247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:31:07+00:00 2026-06-12T22:31:07+00:00

Recently i’ve been trying to create a wait function that waits for 25 ms

  • 0

Recently i’ve been trying to create a wait function that waits for 25 ms using the wall clock as reference. I looked around and found “gettimeofday”, but i’ve been having problems with it. My code (simplified):

while(1)
{
    timeval start, end;
    double t_us;
    bool release = false;    
    while (release == false)
    {
        gettimeofday(&start, NULL);

        DoStuff();

        {
            gettimeofday(&end, NULL);
            t_us = ( (end.tv_sec - start.tv_sec) * 1000*1000) + (end.tv_usec - start.tv_usec);

            if (t_us >= 25000) //25 ms
            {
                release = true;
            }
        }
    }
}

This code runs in a thread (Posix) and, on it’s its own, works fine. DoStuff() is called every 25ms. It does however eat all the CPU if it can (as you might expect) so obviously this isn’t a good idea.

When I tried throttling it by adding a Sleep(1); in the wait loop after the if statement, the entire thing slows by about 50% (that is, it called DoStuff every 37ms or so. This makes no sense to me – assuming DoStuff and any other threads complete their tasks in under (25 – 1) ms the called rate of DoStuff shouldn’t be affected (allowing a 1ms error margin)

I also tried Sleep(0), usleep(1000) and usleep(0) but the behaviour is the same.

The same behaviour occurs whenever another higher priority thread needs CPU time (without the sleep). It’s as if the clock stops counting when the thread reliqnuishes runtime.

I’m aware that gettimeofday is vulnerable to things like NTP updates etc… so I tried using clock_gettime but linking -ltr on my system causes problems so i don’t think that is an option.

Does anyone know what i’m doing wrong?

  • 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-06-12T22:31:09+00:00Added an answer on June 12, 2026 at 10:31 pm

    The part that’s missing here is how the kernel does thread scheduling based on time slices. In rough numbers, if you sleep at the beginning of your time slice for 1ms and the scheduling is done on a 35ms clock rate, your thread may not execute again for 35ms. If you sleep for 40ms, your thread may not execute again for 70ms. You can’t really change that without changing the scheduling, but that’s not recommended due to overall performance implications of the system. You could use a “high-resolution” timer, but often that’s implemented in a tight cycle-wasting loop of “while it’s not time yet, chew CPU” so that’s not really desirable either.

    If you used a high-resolution clock and queried it frequently inside of your DoStuff loop, you could potentially play some tricks like run for 30ms, then do a sleep(1) which could effectively relinquish your thread for the remainder of your timeslice (e.g. 5ms) to let other threads run. Kind of a cooperative/preemptive multitasking if you will. It’s still possible you don’t get back to work for an extended period of time though…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I had the need for a function that I could use to guarantee
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
Recently discovered that using async socket API in C# actually executes the DNS resolve
Recently I was able to create a tablet software for my Cerebral Palsy girl
Recently, I was writing a class in which I discovered that I could reduce
Recently the PHP manual started showing the following warning on every mysql function page:
Recently, I try to build WebKit with VS2012. That cost me a lot of
Recently I am making a server-client program using multithread concept. For some reason, I
Recently I've created one android application using eclipse and ran it using android virtual
Recently I have done an assignment, on overloading basic functionalists(+,-,conjugate...) of complex class using

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.