简体   繁体   中英

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. For example you can use InterlockedOr on windows. In C++0x you can use 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. 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. See: 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. Otherwise you would need to make a work around (for example with Boost).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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