atomic_compare_exchange与大于而不是等于? [英] atomic_compare_exchange with greater-than instead of equals?
问题描述
C ++ 11对原子变量
语义是:
与
预期
所指向的值 c> 等于前者与所需
(执行读 - 修改 - 写操作)。否则,将obj
指向的实际值加载到* expected
(执行加载操作)。
我想这样做,但不是设置 * obj
值等于,我想要在大于另一个(假设我们在讨论有序类型)时设置它。
这是否以某种方式支持?
注意:一个CAS循环不会为我做,因为我比较的值可能会改变非原子操作。根据请求,这里是我的评论作为答案:
我也希望这个存在,但它不,就我所知(当然不是为x86 / x64),除了概念上,和(潜在地)使用多于单个原子指令(其工作但不等待)的解决方案。
C++11 has a 'compare and exchange' operation for atomic variables.
The semantics are:
Atomically compares the value pointed to by
obj
with the value pointed to byexpected
, and if those are equal, replaces the former withdesired
(performs read-modify-write operation). Otherwise, loads the actual value pointed to byobj
into*expected
(performs load operation).
I want to do the same, but instead of setting *obj
when the values are equal, I want it to be set when one is greater-than the other (assume we're talking about an ordered type).
Is this supported somehow? Achievable by some hack perhaps?
Note: A CAS loop will not do for me, since both the values I'm comparing might change between non-atomic operations.
As requested, here's my comment as an answer:
I, too, wish this existed, but it does not, as far as I know (certainly not for x86/x64), apart from conceptually, of course, and workarounds that (potentially) use more than a single atomic instruction (which work but are not wait-free).
这篇关于atomic_compare_exchange与大于而不是等于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!