Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[atomics.order] p8 circularly depend on their own computation is unclear #646

Open
xmh0511 opened this issue Nov 27, 2024 · 0 comments
Open

Comments

@xmh0511
Copy link

xmh0511 commented Nov 27, 2024

Full name of submitter (unless configured in github; will be published with the issue): Jim X

Consider this example:

std::atomic<int> x = 0, y = 2;
// thread 1:
if(y.load(relaxed) == 1){ // #1
   x.store(1,relaxed); // #2
}
//thread 2:
int pre = x.load(relaxed); // #3
while(pre != 0){
   if(x.compare_exchange_strong(pre, pre+1, acquire, relaxed)){  // #4
       break;
   }
}
y.store(1,relaxed); // #5

It is unclear whether the result can boil down to OOTA if both #1 and #4 return true. The loop won't be evaluated when pre equals 0 and the loop will be evaluated otherwise, in both cases, #5 can be eventually executed. The difference might be that the loop could be infinite in the latter case such that #5 won't be reached.

Not sure whether the code in thread 2 is semantically equivalent to

int pre = x.load(relaxed);
if(pre == 0){
   y.store(1,relaxed);
}else{
  while(pre != 0){
     if(x.compare_exchange_strong(pre, pre+1, acquire, relaxed)){  // #4
         break;
     }
  }
   y.store(1,relaxed);
}

such that y.store(1,relaxed) logically depends on the value of pre(i.e. x.load(relaxed)). Or, it semantically equals that y.store(1,relaxed) is irrelevant to whatever value x.load(relaxed) returns because y.store(1,relaxed) would be always executed in any branch. An arguable part is whether y.store(1,relaxed) should be considered as depending on the loop, as said above, the loop might be infinite such that the store to y is unreached.

It is clear that OOTA occurs in the first equivalent transformation, however, it's vague in the second.

Suggested Resolution:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant