简体   繁体   English

比较和交换原子操作与加载链接/存储条件操作

[英]compare-and-swap atomic operation vs Load-link/store-conditional operation

Under an x86 processor I am not sure of the difference between compare-and-swap atomic operation and Load-link/store-conditional operation.在 x86 处理器下,我不确定比较和交换原子操作与加载链接/存储条件操作之间的区别。 Is the latter safer than the former?后者比前者更安全吗? Is it the case that the first is better than the second?是不是第一个比第二个好?

There are three common styles of atomic primitive: Compare-Exchange, Load-Linked/Store-Conditional, and Compare-And-Swap.原子原语有三种常见的 styles:Compare-Exchange、Load-Linked/Store-Conditional 和 Compare-And-Swap。

A CompareExchange operation will atomically read a memory location and, if it matches a compare value, store a specified new value. CompareExchange 操作将自动读取 memory 位置,如果它与比较值匹配,则存储指定的新值。 If the value that was read does not match the compare value, no store takes place.如果读取的值与比较值不匹配,则不进行存储。 In any case, the operation will report the original value read.在任何情况下,操作都会报告读取的原始值。

A Compare-And-Swap operation is similar to CompareExchange, except that it does not report what value was read--merely whether whatever value was read matched the compare value. Compare-And-Swap 操作类似于 CompareExchange,除了它不报告读取的值——仅报告读取的任何值是否与比较值匹配。 Note that a CompareExchange may be used to implement Compare-And-Swap by having it report whether the value read from memory matched the specified compare value.请注意,CompareExchange 可用于实现 Compare-And-Swap,方法是让它报告从 memory 读取的值是否与指定的比较值匹配。

The LL/SC combination allows a store operation to be conditioned upon whether some outside influence might have affected the target since its value was loaded. LL/SC 组合允许存储操作以某些外部影响是否可能影响目标为条件,因为它的值被加载。 In particular, it guarantees that if the store succeeds, the location has not been written at all by outside code.特别是,它保证如果存储成功,则该位置根本没有被外部代码写入。 Even if outside code wrote a new value and then re-wrote the original value, that would be guaranteed to cause the conditional code to fail.即使外部代码写入了一个新值,然后重新写入了原始值,也保证会导致条件代码失败。 Conceptually, this might make LL/SC seem more powerful than other methods, since it wouldn't have the "ABA" problem.从概念上讲,这可能会使 LL/SC 看起来比其他方法更强大,因为它不会有“ABA”问题。 Unfortunately, LL/SC semantics allow for stores to spontaneously fail, and the probability of spontaneously failure may go up rapidly as the complexity of the code between the load and store is increased.不幸的是,LL/SC 语义允许存储自发失败,并且随着加载和存储之间代码复杂性的增加,自发失败的概率可能会迅速上升。 While using LL/SC to implement something like an atomic increment directly would be more efficient than using it to implement a compare-and-swap, and then implementing an atomic increment using that compare-and-swap implementation, in situations where one would need to do much between a load and store, one should generally use LL-SC to implement a compare-and-swap, and then use that compare-and-swap method in a load-modify-CompareAndSwap loop.虽然使用 LL/SC 直接实现类似原子增量的东西比使用它来实现比较和交换,然后在需要的情况下使用该比较和交换实现来实现原子增量更有效要在加载和存储之间做很多事情,通常应该使用 LL-SC 来实现比较和交换,然后在 load-modify-CompareAndSwap 循环中使用该比较和交换方法。

Of the three primitives, the Compare-And-Swap is the least powerful, but it can be implemented in terms of either of the other two.在这三个原语中,Compare-And-Swap 是最不强大的,但它可以根据其他两个中的任何一个来实现。 CompareAndSwap can do a pretty good job of emulating CompareExchange, though there are some corner cases where such emulation might live-lock. CompareAndSwap 可以很好地模拟 CompareExchange,尽管在某些极端情况下,这种模拟可能会锁定。 Neither CompareExchange nor Compare-And-Swap can offer guarantees quite as strong as LL-SC, though the limited amount of code one can reliably place within an LL/SC loop limits the usefulness of its guarantees. CompareExchange 和 Compare-And-Swap 都无法提供与 LL-SC 一样强大的保证,尽管可以可靠地放置在 LL/SC 循环中的有限代码量限制了其保证的有用性。

x86 does not provide LL/SC instructions. x86 不提供 LL/SC 指令。 Check out wikipedia for platforms that do.查看wikipedia上的平台。 Also see this SO question .另请参阅此SO question

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

相关问题 使用 Load-link/store-conditional 来防止 ABA 的无锁 C++11 示例? - Lock-free C++11 example using Load-link/store-conditional to prevent ABA? C++ 原子 CAS(比较和交换)操作不会改变值 - C++ atomic CAS(compare-and-swap) operation does not change value 应该是std :: atomic <int*> :: load正在进行比较和交换循环? - Should std::atomic<int*>::load be doing a compare-and-swap loop? 原子操作传播/可见性(原子负载与原子 RMW 负载) - Atomic operation propagation/visibility (atomic load vs atomic RMW load) 如何通过原子增量和比较交换操作处理整数溢出? - How to handle integer overflows with atomic increment and compare-and-swap operations? 有原子操作吗? - Is there an atomic |= operation? 如何在C ++ 11中使用原子操作fetch_or或test_and_set而不是compare_and_swap在指针中设置位 - How to set a bit in a pointer using atomic operation fetch_or or test_and_set and not compare_and_swap in c++11 std::atomic<> 是否保证 store() 操作立即(几乎)传播到具有 load() 的其他线程? - Does std::atomic<> gurantee that store() operation is propagated immediately (almost) to other threads with load()? 原子比较运算符(无交换) - Atomic Compare Operator (No swap) 对原子变量的非原子操作 - Non atomic operation on atomic variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM