Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between boost::posix_time::milliseconds and boost::chrono::milliseconds

Tags:

c++

boost

I'm trying to use condition_variable_any::timed_wait()

When I pass a boost::chrono::millisecond into the function, it fails to compile:

error: no match for ‘operator+’ in ‘boost::get_system_time() + wait_duration’

However if I pass boost::posix_time::milliseconds into the function it does compile.

The problem is I don't understand the difference between the two. They both claim to be durations. But As I understand posix time, it is representative of the time since the epoch, which to me means that boost::posix_time::milliseconds p(1000) is representative of the time at 1 second after the epoch. I don't see this as a duration.

What am I missing? Can someone please explain the differences?

like image 227
AllenKll Avatar asked Mar 26 '14 17:03

AllenKll


1 Answers

I'll give a stab at it, though I'm not the developer or part of the standards committee.

Boost is a playground for ideas that may one day get integrated into the standard library. As such, it often has quirks in it that you wouldn't see in other places.

boost::posix_time is a wrapper around the posix_time datastructure. POSIX (Portable Operating System Interface) is a set of standards given by IEEE in attempt to get all operating systems and programs to speak in a similar language (spoiler, they don't). POSIX defines a fixed-point numeral system for time and time differences, though the resolution of that structure varies. In BOOST, the resolution is configurable at compile time. The goal of the library is to more easily manipulate POSIX complient time strucures. Introduced before 1.31.

boost::chrono (integrated into C++11 as std::chrono) is a generic time-scale library. The goal was to have a set of functions and structures that can deal with time well and allow the user to configure the underlying datatype. No effort was made to be in line with any standard. Introduced in 1.51.

While this is (100%) two libraries to do the same thing, they do it in a slightly different way. In a system like boost, this is arguably unavoidable: with new language features and programming requirements, you'll probably want to completely refractor some things but need to keep (somewhat) backwards compatibility. But it leads to a weird problem: different implemtation, just to confuse you. Because POSIX time is simply so much older, most interfaces (like condition_variable_any::timed_wai) only have a posix_time interface.

like image 95
IdeaHat Avatar answered Nov 19 '22 23:11

IdeaHat