Boost logo

Boost :

Subject: [boost] Interest in atomic library?
From: Seth LaForge (sethml_at_[hidden])
Date: 2009-04-21 19:32:34


In this day and age, running our C++ programs off fossil fuels makes a
dangerous contribution to global warming and requires purchase of
carbon offsets for ethical programming. Thus I think it's time for a
Boost atomic power library. Oops, sorrry, wrong meaning of atomic.

What would people think of an atomic integer library, along the lines
of Linux's asm/atomic.h? I'd propose a class atomic<T> implementing
operations like atomic read (implicit conversion to T), atomic set
(assignment from T), and compare-and-exchange. For integral types it
would support atomic add and return result (operator+=), atomic
subtract and return result (operator-=), atomic increment and return
result (prefix operator++), and atomic decrement and return result
(prefix operator--).

For arbitrary architectures or unhandled types of T, there would be a
naive implementation using a mutex. On supported architectures there
would be specializations for integral T types, using assembly to
implement more efficient atomic operations. This assembly could
pretty much just be borrowed from the Linux asm/atomic.h headers.

What do people think? I see pros:
  + Cross-platform efficient atomic operations for efficient threaded apps.
I see cons:
  - Hard to predict whether your atomic ops will be efficient (no
specialization for your architecture/T).
  - Some architectures may implement different atomic ops, making the
above ops not the most efficient.

I think the pros outweight the cons. I'd personally much rather use a
Boost.atomic library than asm/atomic.h.

Seth


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk