Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign a vector of atomic types?

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);
      |                            ^
like image 953
ar2015 Avatar asked Jan 01 '20 02:01

ar2015


People also ask

What is the syntax for atomic vectors?

Atomic vectors are linear vectors of a single primitive type, like an STL Vector in C++. There is no useful literal syntax for this fundamental data type. To create one of these stupid beasts, assign like:

How is an atomic vector different from an array?

An atomic vector is different from a one-dimensional array because it has a dim attribute of length while a vector has no such attribute. Atomic vector is also different from the list since the vector items have the same types, while a list can contain various arbitrary data types. There are five types of atomic vectors.

How to create an atomic vector in R?

Atomic data types are the object types that you can use to create atomic vectors. To check if any data object is atomic in R, use the is.atomic () function. Atomic vector is a fundamental data structure in the R programming language.

How to check if any data object is atomic in R?

To check if any data object is atomic in R, use the is.atomic () function. Atomic vector is a fundamental data structure in the R programming language. An atomic vector is different from a one-dimensional array because it has a dim attribute of length while a vector has no such attribute.


1 Answers

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); }
like image 105
Jarod42 Avatar answered Oct 14 '22 12:10

Jarod42