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 cmpxchgReads or writes cannot be reordered with I/O instructions, locked instructions, or serializing instructions
which makes the
Код: Выделить всё
memory_order failКод: Выделить всё
lockExample:
Код: Выделить всё
#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
Мобильная версия