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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:34:21+00:00 2026-05-23T23:34:21+00:00

In my thread (using boost::thread) I need to retrieve the current time in ms

  • 0

In my thread (using boost::thread) I need to retrieve the current time in ms or less and to convert into ms:

Actually, reading here I’ve found this:

tick = boost::posix_time::second_clock::local_time();
now  = boost::posix_time::second_clock::local_time();

And seems to work, but after I need to have a long value of the milliseconds of the now…

How can I do it?

  • 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-23T23:34:21+00:00Added an answer on May 23, 2026 at 11:34 pm

    You can use boost::posix_time::time_duration to get the time range. E.g like this

    boost::posix_time::time_duration diff = tick - now;
    diff.total_milliseconds();
    

    And to get a higher resolution you can change the clock you are using. For example to the boost::posix_time::microsec_clock, though this can be OS dependent. On Windows, for example, boost::posix_time::microsecond_clock has milisecond resolution, not microsecond.

    An example which is a little dependent on the hardware.

    int main(int argc, char* argv[])
    {
        boost::posix_time::ptime t1 = boost::posix_time::second_clock::local_time();
        boost::this_thread::sleep(boost::posix_time::millisec(500));
        boost::posix_time::ptime t2 = boost::posix_time::second_clock::local_time();
        boost::posix_time::time_duration diff = t2 - t1;
        std::cout << diff.total_milliseconds() << std::endl;
    
        boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time();
        boost::this_thread::sleep(boost::posix_time::millisec(500));
        boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();
        boost::posix_time::time_duration msdiff = mst2 - mst1;
        std::cout << msdiff.total_milliseconds() << std::endl;
        return 0;
    }
    

    On my win7 machine. The first out is either 0 or 1000. Second resolution.
    The second one is nearly always 500, because of the higher resolution of the clock. I hope that help a little.

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

Sidebar

Related Questions

i'm in a situation where i need to get boost::thread::id into a variable, but
I started using boost::thread recently (WinXP, VS10, BoostPro) and found that mutex can be
I am experiencing a crash while using the Boost.Spirit and Boost.Thread libraries in my
I am using wxwidgets together with boost::thread. The Thread is a worker thread which
I recently came across the need to sleep the current thread for an exact
I've been using boost::mutex::scoped_lock in this manner: void ClassName::FunctionName() { { boost::mutex::scoped_lock scopedLock(mutex_); //do
So I have done some research, and have found you can create a boost::thread
I have a thread pool that is using shared mutexes from the boost library.
I tried using the boost deadline_timer in this simple test application, but had some
I am using boost::thread, and I meet some problems. The thing is, are there

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.