|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4177" status="New"> |
| 5 | +<title>§[atomics.order] p8 "circularly depend on their own computation" is unclear for loop</title> |
| 6 | +<section><sref ref="[atomics.order]"/></section> |
| 7 | +<submitter>jim x</submitter> |
| 8 | +<date>29 Nov 2024</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +<sref ref="[atomics.order]"/> p8 and p9 gave two paradigmatic examples of how "circularly depend on |
| 14 | +their own computation" means. However, consider this example: |
| 15 | +</p> |
| 16 | +<blockquote><pre> |
| 17 | +std::atomic<int> x = 0, y = 2; |
| 18 | + |
| 19 | +// thread 1: |
| 20 | +if (y.load(relaxed) == 1) { // #1 |
| 21 | + x.store(1, relaxed); // #2 |
| 22 | +} |
| 23 | + |
| 24 | +//thread 2: |
| 25 | +int pre = x.load(relaxed); // #3 |
| 26 | +while (pre != 0) { |
| 27 | + if (x.compare_exchange_strong(pre, pre + 1, acquire, relaxed)) { // #4 |
| 28 | + break; |
| 29 | + } |
| 30 | +} |
| 31 | +y.store(1, relaxed); // #5 |
| 32 | +</pre></blockquote> |
| 33 | +<p> |
| 34 | +when both `#1` and `#3` read `1`, is this a kind of OOTA? `#3` depends on `#2`, `#2` depends on `#1`, |
| 35 | +`#1` depends on `#5`, and the execution of `#5` depends on the exiting of the loop, which in turn initially |
| 36 | +depends on `pre`. |
| 37 | +<p/> |
| 38 | +The loop can never execute, exit after certain iterations, or be a long-time-running without exiting |
| 39 | +(i.e. `cmpxchg` keeps failing). So, it is unclear whether the execution of `#5` depends on the loop. |
| 40 | +However, it resembles the `spin-loop` (a failed `cmpxchg` is a pure load with a relaxed load), and the |
| 41 | +subsequent codes won't execute until the loop exits. So, the scenario of spin-lock seems to agree that |
| 42 | +the code after a loop depends on the loop(regardless of whether the loop can quickly exit or be a |
| 43 | +long-time-run loop). |
| 44 | +<p/> |
| 45 | +From this perspective, the `while` case is something like the `if`, for `if`, the condition is not |
| 46 | +`true`, and the code thereof cannot be executed. Similarly, a code after a while cannot be executed |
| 47 | +if the loop doesn't exit. |
| 48 | +<p/> |
| 49 | +<b>Suggested resolution:</b> |
| 50 | +<p/> |
| 51 | + Either accurately specify what "circularly depend on their own computation" means, or add a paradigmatic |
| 52 | + example regarding loop to indicate what it means. |
| 53 | +</p> |
| 54 | +</discussion> |
| 55 | + |
| 56 | +<resolution> |
| 57 | +<p> |
| 58 | +</p> |
| 59 | +</resolution> |
| 60 | + |
| 61 | +</issue> |
0 commit comments