简体   繁体   English

如何分配原子类型的向量?

[英]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 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);
      |                            ^

std::atomic is neither copyable or move constructible, so you might do instead: std::atomic既不可复制也不可移动构造,因此您可以改为:

std::vector<std::atomic<bool>> myvector(8);
for (auto& b : myvector) { std::atomic_init(&b, false); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM