This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

4177. §[atomics.order] p8 "circularly depend on their own computation" is unclear for loop

Section: 32.5.4 [atomics.order] Status: New Submitter: jim x Opened: 2024-11-29 Last modified: 2024-11-30

Priority: Not Prioritized

View other active issues in [atomics.order].

View all other issues in [atomics.order].

View all issues with New status.

Discussion:

32.5.4 [atomics.order] p8 and p9 gave two paradigmatic examples of how "circularly depend on their own computation" means. However, 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

when both #1 and #3 read 1, is this a kind of OOTA? #3 depends on #2, #2 depends on #1, #1 depends on #5, and the execution of #5 depends on the exiting of the loop, which in turn initially depends on pre.

The loop can never execute, exit after certain iterations, or be a long-time-running without exiting (i.e. cmpxchg keeps failing). So, it is unclear whether the execution of #5 depends on the loop. However, it resembles the spin-loop (a failed cmpxchg is a pure load with a relaxed load), and the subsequent codes won't execute until the loop exits. So, the scenario of spin-lock seems to agree that the code after a loop depends on the loop(regardless of whether the loop can quickly exit or be a long-time-run loop).

From this perspective, the while case is something like the if, for if, the condition is not true, and the code thereof cannot be executed. Similarly, a code after a while cannot be executed if the loop doesn't exit.

Suggested resolution:

Either accurately specify what "circularly depend on their own computation" means, or add a paradigmatic example regarding loop to indicate what it means.

Proposed resolution: