8

How can I assign the members of a vector with an atomic type?

#include <iostream>
#include <thread>
#include <vector>

using namespace std;

int main()
{
    vector<atomic<bool>> myvector;
    int N=8;
    myvector.assign(N,false);
    cout<<"done!"<<endl;
}

https://wandbox.org/permlink/lchfOvqyL3YKNivk

prog.cc: In function 'int main()':
prog.cc:11:28: error: no matching function for call to 'std::vector<std::atomic<bool> >::assign(int&, bool)'
   11 |     myvector.assign(N,false);
      |                            ^
curiousguy
  • 8,038
  • 2
  • 40
  • 58
ar2015
  • 5,558
  • 8
  • 53
  • 110

1 Answers1

8

std::atomic is neither copyable or move constructible, so you might do instead:

std::vector<std::atomic<bool>> myvector(8);
for (auto& b : myvector) { std::atomic_init(&b, false); }
O'Neil
  • 3,790
  • 4
  • 16
  • 30
Jarod42
  • 203,559
  • 14
  • 181
  • 302