我需要std::chrono::high_resolution_clock::time_point字段,我想从一个线程写入,从另一个线程读取。如果我按照原样声明它,我的代码就会编译,而不会有任何错误。
但是为了使我的字段在另一个线程中可见,我用类似于这个std::atomic的std::atomic<std::chrono::high_resolution_clock::time_point>包围它,现在我有了下面的编译错误:
/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;如何声明从一个线程写入并从另一个线程读取的std::chrono::high_resolution_clock::time_point字段(以确保“读取线程”看到最后的值)?
发布于 2015-03-31 09:09:35
你的选择:
std::atomic_ullong)发布于 2016-03-30 18:22:02
使用std::atomic<std::chrono::high_resolution_clock::duration>并在存储时将其设置为time_point::time_since_epoch();在加载时,使用标准的转换构造函数从原子的持续时间开始构造time_point。这有点让人恼火,因为这是必要的,但至少它的类型是安全的,并且原子类型的大小或分辨率没有不确定性。
https://stackoverflow.com/questions/29364456
复制相似问题