20

I find that in practice, with a variety of C++11/C++14 compilers, a std::atomic has an undefined initial value just as it would if it were a "raw" type. That is, we expect that for the expression

int a;

a may have any value. It also turns out to be true that for the expression

std::atomic< int > b;

b may also have any value. To say it another way,

std::atomic< int > b;         // b is undefined

is not equivalent to

std::atomic< int > b{ 0 };    // b == 0

or to

std::atomic< int > b{};       // b == 0

because in the latter two cases b is initialized to a known value.

My question is simple: where in the C++11 or C++14 spec is this behavior documented?

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
OldPeculier
  • 11,049
  • 13
  • 50
  • 76

2 Answers2

17

[atomics.types.generic]/5 says this about integral specializations:

The atomic integral specializations and the specialization atomic shall have standard layout. They shall each have a trivial default constructor and a trivial destructor. They shall each support aggregate initialization syntax.

Moreover, the primary template synopsis at the beginning of the same section normatively specifies the default constructor as:

atomic() noexcept = default;

The effects are defined in [atomic.types.operations]/4 as:

Effects: leaves the atomic object in an uninitialized state.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
7

§ 29.6.5 [atomics.types.operations.req] ¶ 4 of N 4140 (the final draft for C++14) says:

A ::A () noexcept = default;

Effects: leaves the atomic object in an uninitialized state. [ Note: These semantics ensure compatibility with C. — end note ]

Note that this doesn't mean that the object wraps an unspecified value that you can read but not depend on its value. Rather, it means that reading the value out of the object before assigning to it results in undefined behavior.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
  • 5
    Being uninitialized is actually quite important here. Consider what happens if you mmap a block of shared memory from a database file where you are using atomics for locking each BTree node as you descend the tree structure. You certainly don't want your code setting the values to zero when you open the file. – Zan Lynx Mar 31 '16 at 01:06