本文介绍了C ++ - 如何初始化原子能数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 阵列< atomic_size_t,10 -10一个;

无论是 ATOMIC_INIT(A,{0})也不 A = {ATOMIC_VAR_INIT(0)} 似乎工作,返回一个难以理解的错误。你将如何初始化原子能的数组0?

即使是循环的每一步不起作用更新数组的一个元素。什么是原子公司的阵列的目的,如果我们不能初始化呢?

我也想补充一点,我的数组的实际大小是巨大的(不是10比如上例中),所以我需要一个直接初始化。


解决方案

 的std ::阵列< atomic_size_t,10 -10;改编= {0,0,0,0,0,0,0,0,0,0};

或者,如果你可以编译C ++ 11

 的std ::阵列< atomic_size_t,10 -10;改编{{0,0,0,0,0,0,0,0,0,0}};需要//双括号

编辑:

这只是发生,我认为你想储存原子公司,这是不可拷贝,成为这需要他们可拷贝(注集合:我不能在此刻得到我的标准的副本,我知道这适用于其他的收藏品真的,但我不能确定它是否为的std ::阵列以及)。也是如此

有一个类似的问题被张贴了一段时间回来:线程安全无锁阵

array< atomic_size_t, 10 > A;

Neither atomic_init(A,{0}) nor A = {ATOMIC_VAR_INIT(0)} seem to work, returning an unintelligible error. How would you initialise an array of atomics 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 initialise 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 initialisation.

解决方案
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<atomic_size_t, 10> arr{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; // double braces required

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

这篇关于C ++ - 如何初始化原子能数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 15:43