我正在研究 Java 库的 C++ 端口。问题之一是我无法找到 Java 的 AtomicLongArray 的等价物。任何人都知道 c++ 11 中是否已经有任何等效的东西或如何实现类似的功能?我看过 C++11 原子学,但找不到任何东西。
AtomicLongArray 的文档说:
A long array in which elements may be updated atomically. See the java.util.concurrent.atomic
package specification for description of the properties of atomic variables.
这听起来像是一个简单的 std::atomic<long>
数组:
std::array<std::atomic<long>, N> array;
// or, if size is not known at compile time
std::vector<std::atomic<long>> vector(n);
注意只有元素是原子的,容器本身不是,所以不要绕过push_back
进入 vector 。
我是一名优秀的程序员,十分优秀!