Portable Compare And Swap (atomic operations) C/C++ library?

anon picture anon · Jul 21, 2009 · Viewed 31.9k times · Source

Is there any small library, that wrapps various processors' CAS-like operations into macros or functions, that are portable across multiple compilers?

PS. The atomic.hpp library is inside boost::interprocess::detail namespace. The author refuses to make it a public, well maintained library.

Lets reopen the question, and see if there are any other options?

Answer

Dave Goodell picture Dave Goodell · Aug 20, 2009

OPA (Open Portable Atomics) could be a good fit for your needs. https://trac.mcs.anl.gov/projects/openpa/

It provides a consistent C API to common atomic operations across multiple platforms under an MIT-style license. The library is small and certainly meets your size requirements. The current platform list is:

  • GCC inline assembly for x86, x86_64, ia64, PPC 440, and MIPS 5K processors. Several compilers with GCC-compatible-ish front-ends are also supported on the same architectures, such as icc, PGI, and IBM's xlc.
  • GCC atomic intrinsics, so most GCC-4.1+ installations are supported.
  • The SUN Solaris atomic operations library.
  • Windows NT intrinsics (although you currently have to do a little bit of extra work to build on Windows).
  • Two pseudo-platforms, pthread mutex based emulation for portability to otherwise unsupported platforms (while sacrificing some performance), and an "unsafe" implementation for use in code that is conditionally compiled to be single-threaded code.

I've never used it in a C++ program, although it ought to work with little or no changes. I'd be happy to tweak it if you run into trouble (just mail [email protected]).