atomic_compare_exchange与大于而不是等于? [英] atomic_compare_exchange with greater-than instead of equals?

查看:676
本文介绍了atomic_compare_exchange与大于而不是等于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 by expected, and if those are equal, replaces the former with desired (performs read-modify-write operation). Otherwise, loads the actual value pointed to by obj 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆