【发布时间】:2015-06-04 13:05:33
【问题描述】:
我需要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 字段(以确保“读取线程”看到最后一个值)?
【问题讨论】:
-
atomic只能用于可简单复制的类型,据我所知,不能保证time_point可以简单复制。 -
T.C.说是对的。并且你可以使用std::is_trivial or other 测试一下...
标签: c++ multithreading c++11