|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4309" status="New"> |
| 5 | +<title>How are two `seq_cst` operations ordered in the single total order if these two operations are unsequenced?</title> |
| 6 | +<section> |
| 7 | +<sref ref="[atomics.order]"/> |
| 8 | +</section> |
| 9 | +<submitter>jim x</submitter> |
| 10 | +<date>06 Aug 2025</date> |
| 11 | +<priority>99</priority> |
| 12 | + |
| 13 | +<discussion> |
| 14 | +<p> |
| 15 | +Consider this snippet code: |
| 16 | +</p> |
| 17 | +<blockquote><pre> |
| 18 | +std::atomic<int> flag = {0}; |
| 19 | +std::atomic<int> turn = {0}; |
| 20 | + |
| 21 | +if(flag + turn){} |
| 22 | +</pre></blockquote> |
| 23 | +<p> |
| 24 | +The loads of `flag` and `turn` as the operands of `+` are unsequenced according to [intro.execution] p10. |
| 25 | +</p> |
| 26 | +<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;"> |
| 27 | +<p> |
| 28 | +Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. |
| 29 | +</p> |
| 30 | +</blockquote> |
| 31 | +<p> |
| 32 | +However, <sref ref="[atomics.order]"/> p4 says: |
| 33 | +</p> |
| 34 | +<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;"> |
| 35 | +<p> |
| 36 | +There is a single total order <i>S</i> on all `memory_order::seq_cst` operations, including fences, |
| 37 | +that satisfies the following constraints. |
| 38 | +</p></blockquote> |
| 39 | +<p> |
| 40 | +Then, it says that: |
| 41 | +</p> |
| 42 | +<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;"> |
| 43 | +<p> |
| 44 | +First, if <i>A</i> and <i>B</i> are `memory_order::seq_cst` operations and <i>A</i> strongly happens |
| 45 | +before <i>B</i>, then <i>A</i> precedes <i>B</i> in <i>S</i>. |
| 46 | +</p></blockquote> |
| 47 | +<p> |
| 48 | +According to the first sentence, the load of `flag` and the load of `turn` do have an order in the single |
| 49 | +total order <i>S</i> since they are both `memory_order::seq_cst` operations. However, since they are unsequenced, |
| 50 | +the second sentence does not apply to them. So, what's the order of them in <i>S</i>? Is the order of them in |
| 51 | +<i>S</i> unspecified? |
| 52 | +<p/> |
| 53 | +<b>Suggestion Resolution:</b> |
| 54 | +<p/> |
| 55 | +We may want to say the order of such operations is indeterminate in the single total order. That is, |
| 56 | +either <i>A</i> precedes <i>B</i> or <i>B</i> precedes <i>A</i>, but it is unspecified which. |
| 57 | +</p> |
| 58 | +</discussion> |
| 59 | + |
| 60 | +<resolution> |
| 61 | +</resolution> |
| 62 | + |
| 63 | +</issue> |
0 commit comments