Created
December 15, 2014 21:28
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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