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 790239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:39:05+00:00 2026-05-14T21:39:05+00:00

I have a buffer with the UTC time stamp in C, I broadcast that

  • 0

I have a buffer with the UTC time stamp in C, I broadcast that buffer after every ten seconds. The problem is that the time difference between two packets is not consistent. After 5 to 10 iterations the time difference becomes 9, 11 and then again 10. Kindly help me to sort out this problem.

I am using <time.h> for UTC time.

  • 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-14T21:39:06+00:00Added an answer on May 14, 2026 at 9:39 pm

    A thread that sleeps for X milliseconds is not guaranteed to sleep for precisely that many milliseconds. I am assuming that you have a statement that goes something like:

    while(1) {
      ...
      sleep(10); // Sleep for 10 seconds.
      // fetch timestamp and send
    }
    

    You will get a more accurate gauge of time if you sleep for shorter periods (say 20 milliseconds) in a loop checking until the time has expired. When you sleep for 10 seconds, your thread gets moved further out of the immediate scheduling priority of the underlying OS.

    You might also take into account that the time taken to send the timestamps may vary, depending on network conditions, etc, if you do a sleep(10) -> send ->sleep(10) type of loop, the time taken to send will be added onto the next sleep(10) in real terms.

    Try something like this (forgive me, my C is a little rusty):

    bool expired = false;
    double last, current;
    double t1, t2;
    double difference = 0;
    
    while(1) {
       ...
       last = (double)clock();
       while(!expired) {
          usleep(200); // sleep for 20 milliseconds
          current = (double)clock();
          if(((current - last) / (double)CLOCKS_PER_SEC) >= (10.0 - difference))
            expired = true;
       }
       t1 = (double)clock();
       // Set and send the timestamp.
       t2 = (double)clock();
       //
       // Calculate how long it took to send the stamps.
       // and take that away from the next sleep cycle.
       //
       difference = (t2 - t1) / (double)CLOCKS_PER_SEC;
       expired = false;
     }
    

    If you are not bothered about using the standard C library, you could look at using the high resolution timer functionality of windows such as QueryPerformanceFrequency/QueryPerformanceCounter functions.

    LONG_INTEGER freq;
    LONG_INTEGER t2, t1;
    //
    // Get the resolution of the timer.
    //
    QueryPerformanceFrequency(&freq);
    
    // Start Task.
    QueryPerformanceCounter(&t1);
    
    ... Do something ....
    
    QueryPerformanceCounter(&t2);
    
    // Very accurate duration in seconds.
    double duration = (double)(t2.QuadPart - t1.QuadPart) / (double)freq.QuadPart;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.