简体   繁体   English

有原子操作吗?

[英]Is there an atomic |= operation?

Is there such a thing as an atomic |= or and atomic or? 有没有原子|=或原子或? If no what is the recommended technique for setting a bit in an variable that needs to be threadsafe? 如果没有什么建议的技术来设置需要线程安全的变量中的位? (I am avoiding locks) (我在避免锁)

There is not such thing in C++03, but you can use your vendor specific features. 在C ++ 03中没有这样的东西,但您可以使用供应商特定的功能。 For example you can use InterlockedOr on windows. 例如,您可以在Windows上使用InterlockedOr In C++0x you can use atomic_fetch_or . 在C ++ 0x中,您可以使用atomic_fetch_or

Note that atomic operations also require locking, although it's on the hardware level it's still expensive. 请注意,原子操作也需要锁定,尽管它在硬件级别上仍然很昂贵。

您可以使用原子比较和交换(CAS),可以在任何您可能需要的地方使用它,来制作几乎任何操作的原子版本。

Take a look at the _InterlockedOr intrinsic. 看一下_InterlockedOr内在函数。 It's the fastest you can possibly get. 这是你可能获得的最快。

In the current C++ standard there is no such think - but there will be in C++11 which will be released probably in autumn. 在当前的C ++标准中,没有这样的想法 - 但是C ++ 11将会在秋季发布。 See: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1401.pdf 见: http//www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1401.pdf

I am not sure, if there are already compilers supporting parts of the new threading facilities of the upcoming C++ standard. 我不确定,是否已有编译器支持即将推出的C ++标准的新线程设施的部分内容。 Otherwise you would need to make a work around (for example with Boost). 否则你需要做一个解决方法(例如使用Boost)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM