I want to implement TestandSet atomically in c++. what operation is equivalent to xchg instruction in c++
You can use intrinsics functions, depending on your compiler. In gcc for e.g. use __sync_lock_test_and_set (see - http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html)
Alternatively you can use inline assembly and do the locked xchg directly
If you're working in c++11 you can use std::atomic functions. This however provides a higher level of abstraction so the translation to x86 ISA is not one-to-one. See - https://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
On Windows - InterlockedCompareExchange().
In GCC - __sync_bool_compare_and_swap().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With