13

I'd like to document what high-level (i.e. C++ not inline assembler ) functions or macros are available for Compare And Swap (CAS) atomic primitives...

E.g., WIN32 on x86 has a family of functions _InterlockedCompareExchange in the <_intrin.h> header.

Chris Jester-Young
  • 213,251
  • 44
  • 377
  • 423
paxos1977
  • 144,851
  • 26
  • 89
  • 126

7 Answers7

17

I'll let others list the various platform-specific APIs, but for future reference in C++09 you'll get the

atomic_compare_exchange() 

operation in the new "Atomic operations library".

Michael Burr
  • 321,763
  • 49
  • 514
  • 739
9

glib, a common system library on Linux and Unix systems (but also supported on Windows and Mac OS X), defines several atomic operations, including g_atomic_int_compare_and_exchange and g_atomic_pointer_compare_and_exchange.

Ben Combee
  • 16,413
  • 6
  • 38
  • 40
6

GCC has some built-ins for atomic accesses, too.

Randall Cook
  • 6,558
  • 6
  • 31
  • 67
Chris Jester-Young
  • 213,251
  • 44
  • 377
  • 423
2

On Solaris there is "atomic.h" (i.e. <sys/atomic.h>).

Christian.K
  • 44,947
  • 10
  • 92
  • 136
2

MacOS X has OSAtomic.h

Don Neufeld
  • 22,048
  • 10
  • 50
  • 50
2

There have been a series of working group papers on this subject proposing changes to the C++ Standard Library. WG N2427 (C++ Atomic Types and Operations) is the most recent, which contributes to section 29 -- Atomic operations library -- of the pending standard.

seh
  • 14,748
  • 2
  • 49
  • 57
1

java has this CAS operation, too

see here

there are practical uses for this, like a lock-free hashtable used in multiprocessor system

Andreas Petersson
  • 15,982
  • 11
  • 58
  • 91