Skip to content

Instantly share code, notes, and snippets.

@mitliagkas
Created December 15, 2014 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitliagkas/53bd2f9dee655a7322f2 to your computer and use it in GitHub Desktop.
Save mitliagkas/53bd2f9dee655a7322f2 to your computer and use it in GitHub Desktop.
C code segment for Compare and Exchange that is reasonably multiplatform. From: http://stackoverflow.com/questions/13267845/atomic-compare-and-swap-in-assembler-os-independent
#ifdef _MSC_VER
# include <intrin.h>
# define CAS(ptr, oldval, newval) \
_InterlockedCompareExchange(ptr, newval, oldval)
#elif __GNUC__
# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
# error "requires GCC 4.1 or greater"
# endif
# define CAS(ptr, oldval, newval) \
__sync_val_compare_and_swap(ptr, oldval, newval)
#else
# error "CAS not supported on this platform"
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment