std::atomic<int> unique_ids;
void foo() {
int i = unique_ids.fetch_add(1, std::memory_order_relaxed);
std::cout<<i;
}
int main(int argc, char* argv[]) {
std::vector<std::thread> threads;
for (int i = 0; i < 9; ++i) {
threads.emplace_back(foo);
}
for (int i = 0; i < 9; ++i) {
threads[i].join();
}
std::cout << std::endl;
return 0;
}
My aim is to use atomic to generate unique id for concurrency program but I do not care about the orders.
For the above code, my understanding is that the output values in foo should be different although their orders are not guaranteed.
I tested the above code hundred times and all the results are what I expected. I am a beginner of atomic / memory order, could anyone help me clarify my understanding?
Thanks in advance. Aimin
P.S. I would like to point out that this question is not the same as the one c++,std::atomic, what is std::memory_order and how to use them since my question is specifically about the understanding of memory_order_relaxed rather than a generic question about the explanation of atomic and memory order.