#include <atomic>
#include <array>
#include <iostream>
/*
This program demonstrates how to initialize and use an array
of std::atomic<int>. Each element is explicitly set to zero
using store(), which is the standard and safe way to assign
values to atomic objects.
We wrap the initialization logic inside a function to keep
the main routine clean and focused.
*/
// A helper function that initializes all atomic elements to zero.
// Using store() ensures that the write is atomic and follows the
// memory ordering rules of std::atomic.
void initializeAtomicArray(std::array<std::atomic<int>, 10>& arr) {
for (auto& element : arr) {
element.store(0); // atomic write of zero
}
}
// A helper function to print the array values.
// We use load() to safely read atomic values.
void printAtomicArray(const std::array<std::atomic<int>, 10>& arr) {
for (const auto& element : arr) {
std::cout << element.load() << " ";
}
std::cout << "\n";
}
int main() {
// Create an array of atomic integers.
std::array<std::atomic<int>, 10> values;
// Initialize all elements to zero.
initializeAtomicArray(values);
// Display the initialized array.
printAtomicArray(values);
}
/*
run:
0 0 0 0 0 0 0 0 0 0
*/