I need std::chrono::high_resolution_clock::time_point
field which I want to write from one thread and read from another thread. If I declare it as is my code compiles without any errors.
But to make my field visible in another thread I surround it with std::atomic
like this std::atomic<std::chrono::high_resolution_clock::time_point>
and now I have following compilation error:
/usr/include/c++/4.8/atomic:167:7: error: function ‘std::atomic<_Tp>::atomic() [with _Tp = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >]’ defaulted on its first declaration with an exception-specification that differs from the implicit declaration ‘constexpr std::atomic<std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > > >::atomic()’
atomic() noexcept = default;
How should I declare std::chrono::high_resolution_clock::time_point
field which I write from one thread and read from another (to make sure that "reading thread" sees last value)?
Class template std::chrono::time_point represents a point in time. It is implemented as if it stores a value of type Duration indicating the time interval from the start of the Clock 's epoch. Clock must meet the requirements for Clock or be std::chrono::local_t (since C++20).
std::chrono::system_clock::now Returns a time point representing with the current point in time.
Your choices:
forget about making it atomic and use a mutex to serialise access
pick some integral unit of time (e.g. milliseconds since epoch) and convert to/from that on the fly, storing the integral value in some integral type you've worked out has sufficient capacity to cover the range of dates you're handling (perhaps std::atomic_ullong
)
(nutty suggestion removed)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With