Consider the definition of
Код: Выделить всё
compare_and_exchange_strong_explicit
Код: Выделить всё
_Bool atomic_compare_exchange_strong_explicit( volatile A* obj, C* expected, C desired, memory_order succ, memory_order fail );
Код: Выделить всё
memory_order fail
Код: Выделить всё
lock cmpxchg
Reads or writes cannot be reordered with I/O instructions, locked instructions, or serializing instructions
which makes the
Код: Выделить всё
memory_order fail
Код: Выделить всё
lock
Example:
Код: Выделить всё
#include void fail_seqcst(volatile int *p, int *expected, int *desirable){ atomic_compare_exchange_strong_explicit(p, expected, desirable, memory_order_release, memory_order_seq_cst); } void fail_relaxed(volatile int *p, int *expected, int *desirable){ atomic_compare_exchange_strong_explicit(p, expected, desirable, memory_order_release, memory_order_relaxed); }
Код: Выделить всё
fail_relaxed: mov ecx, edx mov eax, DWORD PTR [rsi] lock cmpxchg DWORD PTR [rdi], ecx mov DWORD PTR [rsi], eax sete al movzx eax, al ret fail_seqcst: mov ecx, edx mov eax, DWORD PTR [rsi] lock cmpxchg DWORD PTR [rdi], ecx mov DWORD PTR [rsi], eax sete al movzx eax, al ret
Is there any optimization the compiler may do which would differentiate the code for
Код: Выделить всё
memory_order_relaxed
Код: Выделить всё
memory_order_seq_cst
Код: Выделить всё
x86
Источник: https://stackoverflow.com/questions/781 ... -operation