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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:55:00+00:00 2026-06-02T15:55:00+00:00

I am writing a thread library, and when scheduling the threads I need to

  • 0

I am writing a thread library, and when scheduling the threads I need to know how long they’ve been ready. Every Thread instance has a timeval _timeInReady field, and when I push an instance to the ready queue I call this function:

void Thread::startTiming() {
    gettimeofday(&_timeInReady, NULL);
}

When I want to check the current _timeInReady value I call:

double Thread::getTimeInReady() const {
    return TIME(_timeInReady.tv_sec,_timeInReady.tv_usec);
}

Where TIME is #define TIME(a,b) ((a*1000000) + b) So that I get the total time in microseconds.

My problem is that for some reason, I get crazy negative values (such as -10293843) when I check that field after a while.

Any ideas? Thanks!

  • 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-02T15:55:01+00:00Added an answer on June 2, 2026 at 3:55 pm

    You should return the result as a 64-bit unsigned integer, as you want an integer microsecond result, not a fractional second (which would imply returning double):

    unsigned long long Thread::getTimeInReady() const
    {
        return (unsigned long long)_timeInReady.tv_sec * 1000000ULL +
            (unsigned long long)_timeInReady.tv_usec;
    }
    

    Your system might have uint64_t which is a more concise than unsigned long long, or you could typedef it.

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

Sidebar

Related Questions

I try to learn writing parallel codes with thread library of c++11 so first
I am writing a Windows Form application. It has a thread to perform some
I'm working on a thread library which implement user level threads (i have something
I'm writing a wrapper around a 3rd party library, and it has a method
I'm in the process of writing a library that deals with long-running tasks like
I'm writing a dll library in Delphi with multiple threads created by it. Let
What is the best practice for writing a thread-safe context processor in Django? Say,
I wonder what would happen when I create a joinable thread for writing some
I am writing code that will spawn two thread and then wait for them
I'm writing a game in which a thread - GameThread - loops forever, updating

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.