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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:47:44+00:00 2026-05-23T15:47:44+00:00

I want a boost thread to sleep for some nanoseconds. The following code is

  • 0

I want a boost thread to sleep for some nanoseconds. The following code is a sample that compiles without errors. However, it does not work as expected and I cannot figure out why.

#include <iostream>  
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time.hpp>
//Building options:
//-DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG -lboost_date_time-mt -lboost_thread-mt 
void replay()  
{
    boost::posix_time::time_duration time1, time2; 

    time1=boost::posix_time::seconds(3);
    std::cout << boost::posix_time::to_simple_string(time1) << std::endl;
    boost::this_thread::sleep(time1);

    time2=boost::posix_time::nanoseconds(987654321);
    std::cout << boost::posix_time::to_simple_string(time2) << std::endl;
    boost::this_thread::sleep(time2); 
}
int main(int argc, char* argv[])  
{  
    boost::thread replaythread(replay);  
    replaythread.join();
    return 0;  
}

The BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG is a preprocessor definition required in order to work with nanoseconds (more info). The problems occurs when I set the -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG building option, then the boost::this_thread::sleep does not work for any posix::time_duration. The created thread uses all the CPU and it does not sleep nor process the remaining instructions. If the preprocessor definition is removed, the thread can sleep for any time period unless boost::posix_time::nanoseconds.
The program uses some time_duration variables to store nanoseconds and that makes the boost::this_thread::sleep not work.

Thank you very much for your 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-23T15:47:44+00:00Added an answer on May 23, 2026 at 3:47 pm

    The BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG changes the size of ptime.

    boost::this_thread::sleep is a compiled function, which was compiled (on your distro) without that define, so it expects microsecond-precision ptime arguments. You’re passing nanosecond-precision ptime arguments, and the function fails.

    If you extract the code from the boost library and compile it with this define enabled, the program works as expected:

    #include <iostream>
    #include <boost/thread.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <boost/date_time.hpp>
    
    // the guts of boost_1_46_1/libs/pthread/thread.cpp's version of sleep()
    boost::mutex sleep_mutex;
    boost::condition_variable sleep_condition;
    void mysleep(const boost::posix_time::time_duration& dur)
    {
        boost::system_time st = boost::get_system_time() + dur;
        boost::unique_lock<boost::mutex> lk(sleep_mutex);
        while(sleep_condition.timed_wait(lk, st));
    }
    
    void replay()
    {
        boost::posix_time::time_duration time1, time2;
    
        time1=boost::posix_time::seconds(3);
        std::cout << boost::posix_time::to_simple_string(time1) << std::endl;
        mysleep(time1);
        //boost::this_thread::sleep(time1);
    
        time2=boost::posix_time::nanoseconds(987654321);
        std::cout << boost::posix_time::to_simple_string(time2) << std::endl;
        mysleep(time2);
        //boost::this_thread::sleep(time2);
    }
    int main()
    {
        boost::thread replaythread(replay);
        replaythread.join();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone provide sample pseudocode or share some existing link that has sample code.
I want to compile only the thread and regular expresession library of boost and
I want to use boost::crc so that it works exactly like PHP's crc32() function.
C++0x thread library or Boost.thread define a non-member variadic template function that locks all
I am new to multi-thread programming, I want to implement the following functionality. There
I have programmed a boost::thread application, where I might have some race conditions. I
EDIT : It seems BCP does what I want: Building a subset of boost
My Java library depends on Boost C++ . I've generated the following artifact: boost-maven-project:boost-thread:jar:windows-i386-vs10:1.47.0-b1
So I have done some research, and have found you can create a boost::thread
So, for example, I want to link a boost::thread library, I have to type

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.