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

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


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 opa-discuss@lists.mcs.anl.gov).


The boost interprocess library might be what you are after -- the Atomic.hpp include file contains compare-and-swap implementations for a variety of platforms and compilers.


Intel Threading Building Blocks has a nice portable atomic<T> template which does what you want. But whether it is a small library or not can of course be debated..