I use cmpxchg (comparison and exchange) in the i686 architecture for 32-bit comparison and replacement as follows.
static int CAS(int *ptr, int oldVal, int newVal) { unsigned char ret; __asm__ __volatile__ ( " lock\n" " cmpxchgl %2,%1\n" " sete %0\n" : "=q" (ret), "=m" (*ptr) : "r" (newVal), "m" (*ptr), "a" (oldVal) : "memory");
Which is equivalent for x86_64 architecture for 64-bit comparison and swap
static int CAS(long *ptr, long oldVal, long newVal) { unsigned char ret;
Prabakaran
source share