...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The enumeration boost::memory_order
defines the following
values to represent memory ordering constraints:
Constant |
Description |
---|---|
|
No ordering constraint. Informally speaking, following operations
may be reordered before, preceding operations may be reordered
after the atomic operation. This constraint is suitable only when
either a) further operations do not depend on the outcome of the
atomic operation or b) ordering is enforced through stand-alone
|
|
Perform |
|
Perform |
|
Perform |
|
Perform both |
|
Enforce sequential consistency. Implies |
See section happens-before for explanation of the various ordering constraints.
boost::atomic<T>
provides methods
for atomically accessing variables of a suitable type T
.
The type is suitable if it satisfies one of the following constraints:
class
or struct
)
that has a non-throwing default constructor, that is copyable via memcpy
and comparable via memcmp
.
Note that all classes having a trivial default constructor, no destructor
and no virtual methods satisfy the second condition according to C++98. On
a given platform, other data-types may also satisfy
this constraint, however you should exercise caution as the behaviour becomes
implementation-defined. Also be warned that structures with "padding"
between data members may compare non-equal via memcmp
even though all members are equal.
All atomic objects supports the following operations:
Syntax |
Description |
---|---|
|
Initialize to an unspecified value |
|
Initialize to |
|
Checks if the atomic object is lock-free |
|
Return current value |
|
Write new value to atomic variable |
|
Exchange current value with |
|
Compare current value with |
|
Compare current value with |
|
Compare current value with |
|
Compare current value with |
order
always has memory_order_seq_cst
as default parameter.
The compare_exchange_weak
/compare_exchange_strong
variants taking
four parameters differ from the three parameter variants in that they allow
a different memory ordering constraint to be specified in case the operation
fails.
In addition to these explicit operations, each atomic<T>
object also supports implicit store
and load
through the use of "assignment" and "conversion to T
"
operators. Avoid using these operators, as they do not allow explicit specification
of a memory ordering constraint.
In addition to the operations listed in the previous section, boost::atomic<I>
for integral types I
supports the
following operations:
Syntax |
Description |
---|---|
|
Add |
|
Subtract |
|
Apply bit-wise "and" with |
|
Apply bit-wise "or" with |
|
Apply bit-wise "xor" with |
order
always has memory_order_seq_cst
as default parameter.
In addition to these explicit operations, each boost::atomic<I>
object also supports implicit pre-/post- increment/decrement, as well as
the operators +=
, -=
, &=
,
|=
and ^=
.
Avoid using these operators, as they do not allow explicit specification
of a memory ordering constraint.
In addition to the operations applicable to all atomic object, boost::atomic<P>
for pointer types P
(other than
void
pointers) support the following operations:
Syntax |
Description |
---|---|
|
Add |
|
Subtract |
order
always has memory_order_seq_cst
as default parameter.
In addition to these explicit operations, each boost::atomic<P>
object also supports implicit pre-/post- increment/decrement, as well as
the operators +=
, -=
. Avoid using these operators, as they
do not allow explicit specification of a memory ordering constraint.
Syntax |
Description |
---|---|
|
Issue fence for coordination with other threads. |
|
Issue fence for coordination with signal handler (only in same thread). |
Boost.Atomic defines a number of macros
to allow compile-time detection whether an atomic data type is implemented
using "true" atomic operations, or whether an internal "lock"
is used to provide atomicity. The following macros will be defined to 0
if operations on the data type always require
a lock, to 1
if operations on
the data type may sometimes require a lock, and to 2
if they are always lock-free:
Macro |
Description |
---|---|
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |