Home | Libraries | People | FAQ | More |
The library contains header-only and compiled parts. The library is header-only for lock-free cases but requires a separate binary to implement the lock-based emulation. Users are able to detect whether linking to the compiled part is required by checking the feature macros.
The following macros affect library behavior:
Macro |
Description |
---|---|
|
Affects 32-bit x86 Oracle Studio builds. When defined, the library
assumes the target CPU does not support |
|
Affects 64-bit x86 MSVC and Oracle Studio builds. When defined,
the library assumes the target CPU does not support |
|
Affects 32-bit x86 Oracle Studio builds. When defined, the library
assumes the target CPU does not support |
|
When defined, all operations are implemented with locks. This is mostly used for testing and should not be used in real world projects. |
|
Control library linking. If defined, the library assumes dynamic linking, otherwise static. The latter macro affects all Boost libraries, not just Boost.Atomic. |
|
Control library auto-linking on Windows. When defined, disables auto-linking. The latter macro affects all Boost libraries, not just Boost.Atomic. |
Besides macros, it is important to specify the correct compiler options for the target CPU. With GCC and compatible compilers this affects whether particular atomic operations are lock-free or not.
Boost building process is described in the Getting Started guide. For example, you can build Boost.Atomic with the following command line:
bjam --with-atomic variant=release instruction-set=core2 stage
#include <boost/memory_order.hpp>
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.
#include <boost/atomic/atomic.hpp>
boost::atomic<T>
provides methods
for atomically accessing variables of a suitable type T
.
The type is suitable if it is trivially copyable (3.9/9
[basic.types]). Following are examples of the types compatible with this
requirement:
class
or struct
that has no non-trivial
copy or move constructors or assignment operators, has a trivial destructor,
and that is comparable via memcmp
.
Note that classes with virtual functions or virtual base classes do not satisfy
the requirements. 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.
#include <boost/atomic/fences.hpp>
Syntax |
Description |
---|---|
|
Issue fence for coordination with other threads. |
|
Issue fence for coordination with signal handler (only in same thread). |
#include <boost/atomic/capabilities.hpp>
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 |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
In addition to these standard macros, Boost.Atomic
also defines a number of extension macros, which can also be useful. Like
the standard ones, these macros are defined to values 0
,
1
and 2
to indicate whether the corresponding operations are lock-free or not.
Macro |
Description |
---|---|
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Indicate whether |
|
Defined after including |
In the table above, intN_type
is a type that fits storage of contiguous N
bits, suitably aligned for atomic operations.