Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::atomic<std::chrono::high_resolution_clock::time_point> can not compile

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)?

like image 350
Oleg Vazhnev Avatar asked Mar 31 '15 08:03

Oleg Vazhnev


People also ask

What is std :: Chrono :: Time_point?

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).

What does std :: Chrono :: System_clock :: now return?

std::chrono::system_clock::now Returns a time point representing with the current point in time.


1 Answers

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)

like image 108
Tony Delroy Avatar answered Sep 19 '22 17:09

Tony Delroy