Cloud Stack Ninja

I would like to know how to make the following function in a whole atomic.

With my code, I believe there can be a situation that two threads both pass the condition, and return 0,1 respectively right?

static std::atomic<uintV> shared_v (0);
int compare_increment() {
  if (shared_v >= 10) {
    return -1;
  } 
  return shared_v++;

Any help would be appreciated.



Read more here: https://stackoverflow.com/questions/64406868/c-atomic-variable-compare-and-increment

Content Attribution

This content was originally published by Baup at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: