首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::atomic<std::chrono::high_resolution_clock::time_point>无法编译

std::atomic<std::chrono::high_resolution_clock::time_point>无法编译
EN

Stack Overflow用户
提问于 2015-03-31 08:52:58
回答 2查看 6.9K关注 0票数 18

我需要std::chrono::high_resolution_clock::time_point字段,我想从一个线程写入,从另一个线程读取。如果我按照原样声明它,我的代码就会编译,而不会有任何错误。

但是为了使我的字段在另一个线程中可见,我用类似于这个std::atomicstd::atomic<std::chrono::high_resolution_clock::time_point>包围它,现在我有了下面的编译错误:

代码语言:javascript
复制
/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字段(以确保“读取线程”看到最后的值)?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-31 09:09:35

你的选择:

  • 忘记让它是原子的,并使用互斥对象来序列化访问。
  • 选择一些积分的时间单位(例如,从时代开始的毫秒),并在飞行中转换为/从这个单位转换,将积分值存储在你计算出来的某种积分类型中,有足够的能力覆盖你正在处理的日期的范围(也许是std::atomic_ullong)
  • (疯狂的建议被删除)
票数 16
EN

Stack Overflow用户

发布于 2016-03-30 18:22:02

使用std::atomic<std::chrono::high_resolution_clock::duration>并在存储时将其设置为time_point::time_since_epoch();在加载时,使用标准的转换构造函数从原子的持续时间开始构造time_point。这有点让人恼火,因为这是必要的,但至少它的类型是安全的,并且原子类型的大小或分辨率没有不确定性。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29364456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档