如何在 std::atomic<bool> 上 运行 compare_exchange_weak?
How to run compare_exchange_weak on an std::atomic<bool>?
我试过了运行
std::atomic<bool> set(false);
然后在一个函数中我尝试使用:
Process::signalsDone.compare_exchange_weak(false, true, std::memory_order_release, std::memory_order_relaxed);
为什么编译时会出现错误消息?
error C2664: 'bool std::atomic<bool>::compare_exchange_weak(_Ty &,const _Ty,const std::memory_order,const std::memory_order) noexcept': cannot convert argument 1 from 'bool' to '_Ty &'
with
[
_Ty=bool
]
C:\Program Files (x86)\Microsoft Visual Studio19\Professional\VC\Tools\MSVC.27.29110\include\atomic(1636): note: see declaration of 'std::atomic<bool>::compare_exchange_weak'
compare_exchange_weak
的第一个参数is a T&
。
您不能将 false
之类的文字绑定到 T&
;你从来没有做到过。
相反,创建一个可以通过引用传递给函数的 bool
类型的变量。如果比较失败,变量的值将更改为 atomic_bool
的实际值。
我试过了运行
std::atomic<bool> set(false);
然后在一个函数中我尝试使用:
Process::signalsDone.compare_exchange_weak(false, true, std::memory_order_release, std::memory_order_relaxed);
为什么编译时会出现错误消息?
error C2664: 'bool std::atomic<bool>::compare_exchange_weak(_Ty &,const _Ty,const std::memory_order,const std::memory_order) noexcept': cannot convert argument 1 from 'bool' to '_Ty &'
with
[
_Ty=bool
]
C:\Program Files (x86)\Microsoft Visual Studio19\Professional\VC\Tools\MSVC.27.29110\include\atomic(1636): note: see declaration of 'std::atomic<bool>::compare_exchange_weak'
compare_exchange_weak
的第一个参数is a T&
。
您不能将 false
之类的文字绑定到 T&
;你从来没有做到过。
相反,创建一个可以通过引用传递给函数的 bool
类型的变量。如果比较失败,变量的值将更改为 atomic_bool
的实际值。