译 - Understand `std::atomic::compare_exchange_weak()` in C++11
原文是stackoverflow上的一个关于
compare_exchange_weak()
问题和相应的答案。
Question
|
|
compare_exchange_weak()
是C++11中提供的compare-exchange原语之一。之所以是weak,是因为即使在对象的值等于expected
的情况下,也返回false。这是因为在某些平台上的spurious failure,这些平台使用了一系列的指令(而不是像在x86上一样,使用单条的指令)来实现CAS。在这种平台上,context switch, reloading of the same address (or cache line) by another thread等,将会导致这条原语失败。由于不是因为对象的值(不等于expected
)导致的操作失败,因此是spurious
。相反的,it’s kind of timing issues。