开发者

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.


You can use an atomic compare and swap (CAS), available everywhere you're likely to need it, to make an atomic version of pretty much any operation.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜