Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding memory order relaxed in C++

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.

like image 590
Aimin Huang Avatar asked Sep 25 '16 14:09

Aimin Huang


1 Answers

This is a legitimate use of relaxed memory ordering. You just need the operation to be atomic with respect to other accesses to the very same atomic. Every atomic operation has that characteristic regardless of memory ordering or it wouldn't be atomic at all.

like image 125
David Schwartz Avatar answered Oct 13 '22 01:10

David Schwartz