8
std::array< std::atomic_size_t, 10 > A;
// ...
std::atomic_init(A, {0}); // error
A = {ATOMIC_VAR_INIT(0)}; // error

How would you initialize an array of std::atomic to 0s?

Even for loops updating one element of the array at every step does not work. What is the purpose of arrays of atomics if we can't initialize them?

I would also like to add that the actual size of my array is huge (not 10 like in the example), so I would need a direct-initialization.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Pippo
  • 1,543
  • 3
  • 21
  • 40

2 Answers2

5
std::array<std::atomic<std::size_t>, 100> A;
for(auto&x:A)
  std::atomic_init(&x,std::size_t(0));

does the job using

clang++ -std=c++11 -stdlib=libc++ -Weverything -Wno-c++98-compat

using clang-3.3. I also tried with gcc 4.8, but it doesn't support std::atomic_init(). However, I suppose you can replace std::atomic_init(&x,std::size_t(0)) with x=std::size_t(0).

Note that std::atomic<> is not copyable, which breaks some container methods (including construction of std::array<std::atomic<T>> from a T). Also, storing atomics in an array may cause false sharing, affecting performance.

EDIT 2019

The code in the accepted answer by Zac Howland does not compile (neither with clang nor with gcc). Here is a version that will

struct foo
{
    std::array<std::atomic_size_t,2> arr= {{{0},{0}}};
    std::atomic_size_t arr_alt[2] = {{0},{0}};
};
Walter
  • 44,150
  • 20
  • 113
  • 196
  • Unfortunately, `atomic_init` has been deprecated in C++20 :( See http://www.wg21.link/p0883 – dyp Jul 17 '20 at 14:53
3
std::array<atomic_size_t, 10> arr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

or if you can compile for C++11

std::array<std::atomic_size_t, 10> arr{{{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} }}; // double braces required

Example: https://www.ideone.com/Mj9kfE

Edit:

It just occurred to me that you are trying to store atomics, which are not copyable, into a collection that would require they be copyable (Note: I can't get to my copy of the standard at the moment. I know this holds true for the other collections, but I'm unsure if it holds true for std::array as well).

A similar problem was posted a while back: Thread-safe lock-free array

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • Ok, but I put 10 as an example. Actually, I should initialise my array with a huge number of zeros. – Pippo Oct 17 '13 at 19:08
  • Then it's not possible to have a collection of atomic variables?! o.O – Pippo Oct 17 '13 at 19:14
  • Sort of. You have to jump through a couple small hoops. The other SO question has an example of how to do it. – Zac Howland Oct 17 '13 at 19:20
  • The fact is that I noticed the question you linked before posting mine, but naively I thought they wanted to do something more advanced. Since I "simply" wanted to initialise a static array of atomics, I assumed there would have been an easier way. Also because somewhere on the Internet people define arrays of atomics like I did; now I cannot imagine how they use them. – Pippo Oct 17 '13 at 19:49
  • Sorry, had to downvote, answer is wrong. See my answer below – Walter Dec 06 '19 at 14:31
  • @Walter At one point in time, it did compile, but it seems it needs an extra set of curly braces now. I've updated the answer. – Zac Howland Oct 26 '20 at 04:17