tmp/tmp4f5hamz0/{from.md → to.md}
RENAMED
|
@@ -67,32 +67,24 @@ Terms that are used only in a small portion of this International
|
|
| 67 |
Standard are defined where they are used and italicized where they are
|
| 68 |
defined.
|
| 69 |
|
| 70 |
#### 1 argument <a id="defns.argument">[[defns.argument]]</a>
|
| 71 |
|
| 72 |
-
actual argument
|
| 73 |
-
actual parameter
|
| 74 |
\<function call expression\> expression in the comma-separated list
|
| 75 |
bounded by the parentheses
|
| 76 |
|
| 77 |
#### 2 argument <a id="defns.argument.macro">[[defns.argument.macro]]</a>
|
| 78 |
|
| 79 |
-
actual argument
|
| 80 |
-
actual parameter
|
| 81 |
\<function-like macro\> sequence of preprocessing tokens in the
|
| 82 |
comma-separated list bounded by the parentheses
|
| 83 |
|
| 84 |
#### 3 argument <a id="defns.argument.throw">[[defns.argument.throw]]</a>
|
| 85 |
|
| 86 |
-
actual argument
|
| 87 |
-
actual parameter
|
| 88 |
\<throw expression\> the operand of `throw`
|
| 89 |
|
| 90 |
#### 4 argument <a id="defns.argument.templ">[[defns.argument.templ]]</a>
|
| 91 |
|
| 92 |
-
actual argument
|
| 93 |
-
actual parameter
|
| 94 |
\<template instantiation\> expression, *type-id* or *template-name* in
|
| 95 |
the comma-separated list bounded by the angle brackets
|
| 96 |
|
| 97 |
#### 5 conditionally-supported <a id="defns.cond.supp">[[defns.cond.supp]]</a>
|
| 98 |
|
|
@@ -143,28 +135,22 @@ character set of either the source or the execution environment
|
|
| 143 |
The extended character set is a superset of the basic character set (
|
| 144 |
[[lex.charset]]).
|
| 145 |
|
| 146 |
#### 14 parameter <a id="defns.parameter">[[defns.parameter]]</a>
|
| 147 |
|
| 148 |
-
formal argument
|
| 149 |
-
formal parameter
|
| 150 |
\<function or catch clause\> object or reference declared as part of a
|
| 151 |
function declaration or definition or in the catch clause of an
|
| 152 |
exception handler that acquires a value on entry to the function or
|
| 153 |
handler
|
| 154 |
|
| 155 |
#### 15 parameter <a id="defns.parameter.macro">[[defns.parameter.macro]]</a>
|
| 156 |
|
| 157 |
-
formal argument
|
| 158 |
-
formal parameter
|
| 159 |
\<function-like macro\> identifier from the comma-separated list bounded
|
| 160 |
by the parentheses immediately following the macro name
|
| 161 |
|
| 162 |
#### 16 parameter <a id="defns.parameter.templ">[[defns.parameter.templ]]</a>
|
| 163 |
|
| 164 |
-
formal argument
|
| 165 |
-
formal parameter
|
| 166 |
\<template\> *template-parameter*
|
| 167 |
|
| 168 |
#### 17 signature <a id="defns.signature">[[defns.signature]]</a>
|
| 169 |
|
| 170 |
\<function\> name, parameter type list ([[dcl.fct]]), and enclosing
|
|
@@ -479,11 +465,12 @@ characteristics and behavior in these respects.[^6] Such documentation
|
|
| 479 |
shall define the instance of the abstract machine that corresponds to
|
| 480 |
that implementation (referred to as the “corresponding instance” below).
|
| 481 |
|
| 482 |
Certain other aspects and operations of the abstract machine are
|
| 483 |
described in this International Standard as unspecified (for example,
|
| 484 |
-
|
|
|
|
| 485 |
International Standard defines a set of allowable behaviors. These
|
| 486 |
define the nondeterministic aspects of the abstract machine. An instance
|
| 487 |
of the abstract machine can thus have more than one possible execution
|
| 488 |
for a given program and a given input.
|
| 489 |
|
|
@@ -498,19 +485,15 @@ of the corresponding instance of the abstract machine with the same
|
|
| 498 |
program and the same input. However, if any such execution contains an
|
| 499 |
undefined operation, this International Standard places no requirement
|
| 500 |
on the implementation executing that program with that input (not even
|
| 501 |
with regard to operations preceding the first undefined operation).
|
| 502 |
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
are unspecified during the execution of the signal handler, and the
|
| 510 |
-
value of any object not in either of these two categories that is
|
| 511 |
-
modified by the handler becomes undefined.
|
| 512 |
|
| 513 |
An instance of each object with automatic storage duration (
|
| 514 |
[[basic.stc.auto]]) is associated with each entry into its block. Such
|
| 515 |
an object exists and retains its last-stored value during the execution
|
| 516 |
of the block and while the block is suspended (by a call of a function
|
|
@@ -578,18 +561,20 @@ or -17 and 12. However on a machine in which overflows do not produce an
|
|
| 578 |
exception and in which the results of overflows are reversible, the
|
| 579 |
above expression statement can be rewritten by the implementation in any
|
| 580 |
of the above ways because the same result will occur.
|
| 581 |
|
| 582 |
A *full-expression* is an expression that is not a subexpression of
|
| 583 |
-
another expression.
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
call
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
|
|
|
|
|
|
| 591 |
|
| 592 |
``` cpp
|
| 593 |
struct S {
|
| 594 |
S(int i): I(i) { }
|
| 595 |
int& v() { return I; }
|
|
@@ -640,22 +625,25 @@ sequenced* when either *A* is sequenced before *B* or *B* is sequenced
|
|
| 640 |
before *A*, but it is unspecified which. Indeterminately sequenced
|
| 641 |
evaluations cannot overlap, but either could be executed first.
|
| 642 |
|
| 643 |
Every value computation and side effect associated with a
|
| 644 |
full-expression is sequenced before every value computation and side
|
| 645 |
-
effect associated with the next full-expression to be evaluated.[^8]
|
| 646 |
|
| 647 |
Except where noted, evaluations of operands of individual operators and
|
| 648 |
of subexpressions of individual expressions are unsequenced. In an
|
| 649 |
expression that is evaluated more than once during the execution of a
|
| 650 |
program, unsequenced and indeterminately sequenced evaluations of its
|
| 651 |
subexpressions need not be performed consistently in different
|
| 652 |
evaluations. The value computations of the operands of an operator are
|
| 653 |
sequenced before the value computation of the result of the operator. If
|
| 654 |
a side effect on a scalar object is unsequenced relative to either
|
| 655 |
another side effect on the same scalar object or a value computation
|
| 656 |
-
using the value of the same scalar object,
|
|
|
|
|
|
|
|
|
|
| 657 |
|
| 658 |
``` cpp
|
| 659 |
void f(int, int);
|
| 660 |
void g(int i, int* v) {
|
| 661 |
i = v[i++]; // the behavior is undefined
|
|
@@ -705,16 +693,41 @@ execution can be viewed as an interleaving of all its threads. However,
|
|
| 705 |
some kinds of atomic operations, for example, allow executions
|
| 706 |
inconsistent with a simple interleaving, as described below. Under a
|
| 707 |
freestanding implementation, it is *implementation-defined* whether a
|
| 708 |
program can have more than one thread of execution.
|
| 709 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 710 |
Implementations should ensure that all unblocked threads eventually make
|
| 711 |
progress. Standard library functions may silently block on I/O or locks.
|
| 712 |
Factors in the execution environment, including externally-imposed
|
| 713 |
thread priorities, may prevent an implementation from making certain
|
| 714 |
guarantees of forward progress.
|
| 715 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
The value of an object visible to a thread *T* at a particular point is
|
| 717 |
the initial value of the object, a value assigned to the object by *T*,
|
| 718 |
or a value assigned to the object by another thread, according to the
|
| 719 |
rules below. In some cases, there may instead be undefined behavior.
|
| 720 |
Much of this section is motivated by the desire to support atomic
|
|
@@ -864,19 +877,15 @@ or undefined. This states that operations on ordinary objects are not
|
|
| 864 |
visibly reordered. This is not actually detectable without data races,
|
| 865 |
but it is necessary to ensure that data races, as defined below, and
|
| 866 |
with suitable restrictions on the use of atomics, correspond to data
|
| 867 |
races in a simple interleaved (sequentially consistent) execution.
|
| 868 |
|
| 869 |
-
The
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
atomic object *M*, as determined by evaluation *B*, shall be the value
|
| 875 |
-
stored by some operation in the visible sequence of *M* with respect to
|
| 876 |
-
*B*. It can be shown that the visible sequence of side effects of a
|
| 877 |
-
value computation is unique given the coherence requirements below.
|
| 878 |
|
| 879 |
If an operation *A* that modifies an atomic object *M* happens before an
|
| 880 |
operation *B* that modifies *M*, then *A* shall be earlier than *B* in
|
| 881 |
the modification order of *M*. This requirement is known as write-write
|
| 882 |
coherence.
|
|
@@ -887,13 +896,13 @@ value computation *B* of *M*, and *A* takes its value from a side effect
|
|
| 887 |
stored by *X* or the value stored by a side effect *Y* on *M*, where *Y*
|
| 888 |
follows *X* in the modification order of *M*. This requirement is known
|
| 889 |
as read-read coherence.
|
| 890 |
|
| 891 |
If a value computation *A* of an atomic object *M* happens before an
|
| 892 |
-
operation *B*
|
| 893 |
-
*X* on *M*, where *X* precedes *B* in the modification order
|
| 894 |
-
This requirement is known as read-write coherence.
|
| 895 |
|
| 896 |
If a side effect *X* on an atomic object *M* happens before a value
|
| 897 |
computation *B* of *M*, then the evaluation *B* shall take its value
|
| 898 |
from *X* or from a side effect *Y* that follows *X* in the modification
|
| 899 |
order of *M*. This requirement is known as write-read coherence.
|
|
@@ -901,22 +910,29 @@ order of *M*. This requirement is known as write-read coherence.
|
|
| 901 |
The four preceding coherence requirements effectively disallow compiler
|
| 902 |
reordering of atomic operations to a single object, even if both
|
| 903 |
operations are relaxed loads. This effectively makes the cache coherence
|
| 904 |
guarantee provided by most hardware available to C++atomic operations.
|
| 905 |
|
| 906 |
-
The
|
| 907 |
-
relation, which depends on the values observed by loads of
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 913 |
|
| 914 |
The execution of a program contains a *data race* if it contains two
|
| 915 |
-
|
| 916 |
-
atomic, and neither happens before the other
|
| 917 |
-
|
|
|
|
| 918 |
mutexes and `memory_order_seq_cst` operations to prevent all data races
|
| 919 |
and use no other synchronization operations behave as if the operations
|
| 920 |
executed by their constituent threads were simply interleaved, with each
|
| 921 |
value computation of an object being taken from the last side effect on
|
| 922 |
that object in that interleaving. This is normally referred to as
|
|
@@ -925,20 +941,29 @@ programs, and data-race-free programs cannot observe most program
|
|
| 925 |
transformations that do not change single-threaded program semantics. In
|
| 926 |
fact, most single-threaded program transformations continue to be
|
| 927 |
allowed, since any program that behaves differently as a result must
|
| 928 |
perform an undefined operation.
|
| 929 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 930 |
Compiler transformations that introduce assignments to a potentially
|
| 931 |
shared memory location that would not be modified by the abstract
|
| 932 |
machine are generally precluded by this standard, since such an
|
| 933 |
assignment might overwrite another assignment by a different thread in
|
| 934 |
cases in which an abstract machine execution would not have encountered
|
| 935 |
a data race. This includes implementations of data member assignment
|
| 936 |
that overwrite adjacent members in separate memory locations. Reordering
|
| 937 |
of atomic loads in cases in which the atomics in question may alias is
|
| 938 |
-
also generally precluded, since this may violate the
|
| 939 |
-
rules.
|
| 940 |
|
| 941 |
Transformations that introduce a speculative read of a potentially
|
| 942 |
shared memory location may not preserve the semantics of the C++program
|
| 943 |
as defined in this standard, since they potentially introduce a data
|
| 944 |
race. However, they are typically valid in the context of an optimizing
|
|
@@ -982,10 +1007,11 @@ Electronic Engineers, Inc.
|
|
| 982 |
|
| 983 |
All rights in these originals are reserved.
|
| 984 |
|
| 985 |
<!-- Link reference definitions -->
|
| 986 |
[atomics]: atomics.md#atomics
|
|
|
|
| 987 |
[atomics.lockfree]: atomics.md#atomics.lockfree
|
| 988 |
[atomics.order]: atomics.md#atomics.order
|
| 989 |
[basic]: basic.md#basic
|
| 990 |
[basic.compound]: basic.md#basic.compound
|
| 991 |
[basic.def]: basic.md#basic.def
|
|
|
|
| 67 |
Standard are defined where they are used and italicized where they are
|
| 68 |
defined.
|
| 69 |
|
| 70 |
#### 1 argument <a id="defns.argument">[[defns.argument]]</a>
|
| 71 |
|
|
|
|
|
|
|
| 72 |
\<function call expression\> expression in the comma-separated list
|
| 73 |
bounded by the parentheses
|
| 74 |
|
| 75 |
#### 2 argument <a id="defns.argument.macro">[[defns.argument.macro]]</a>
|
| 76 |
|
|
|
|
|
|
|
| 77 |
\<function-like macro\> sequence of preprocessing tokens in the
|
| 78 |
comma-separated list bounded by the parentheses
|
| 79 |
|
| 80 |
#### 3 argument <a id="defns.argument.throw">[[defns.argument.throw]]</a>
|
| 81 |
|
|
|
|
|
|
|
| 82 |
\<throw expression\> the operand of `throw`
|
| 83 |
|
| 84 |
#### 4 argument <a id="defns.argument.templ">[[defns.argument.templ]]</a>
|
| 85 |
|
|
|
|
|
|
|
| 86 |
\<template instantiation\> expression, *type-id* or *template-name* in
|
| 87 |
the comma-separated list bounded by the angle brackets
|
| 88 |
|
| 89 |
#### 5 conditionally-supported <a id="defns.cond.supp">[[defns.cond.supp]]</a>
|
| 90 |
|
|
|
|
| 135 |
The extended character set is a superset of the basic character set (
|
| 136 |
[[lex.charset]]).
|
| 137 |
|
| 138 |
#### 14 parameter <a id="defns.parameter">[[defns.parameter]]</a>
|
| 139 |
|
|
|
|
|
|
|
| 140 |
\<function or catch clause\> object or reference declared as part of a
|
| 141 |
function declaration or definition or in the catch clause of an
|
| 142 |
exception handler that acquires a value on entry to the function or
|
| 143 |
handler
|
| 144 |
|
| 145 |
#### 15 parameter <a id="defns.parameter.macro">[[defns.parameter.macro]]</a>
|
| 146 |
|
|
|
|
|
|
|
| 147 |
\<function-like macro\> identifier from the comma-separated list bounded
|
| 148 |
by the parentheses immediately following the macro name
|
| 149 |
|
| 150 |
#### 16 parameter <a id="defns.parameter.templ">[[defns.parameter.templ]]</a>
|
| 151 |
|
|
|
|
|
|
|
| 152 |
\<template\> *template-parameter*
|
| 153 |
|
| 154 |
#### 17 signature <a id="defns.signature">[[defns.signature]]</a>
|
| 155 |
|
| 156 |
\<function\> name, parameter type list ([[dcl.fct]]), and enclosing
|
|
|
|
| 465 |
shall define the instance of the abstract machine that corresponds to
|
| 466 |
that implementation (referred to as the “corresponding instance” below).
|
| 467 |
|
| 468 |
Certain other aspects and operations of the abstract machine are
|
| 469 |
described in this International Standard as unspecified (for example,
|
| 470 |
+
evaluation of expressions in a *new-initializer* if the allocation
|
| 471 |
+
function fails to allocate memory ([[expr.new]])). Where possible, this
|
| 472 |
International Standard defines a set of allowable behaviors. These
|
| 473 |
define the nondeterministic aspects of the abstract machine. An instance
|
| 474 |
of the abstract machine can thus have more than one possible execution
|
| 475 |
for a given program and a given input.
|
| 476 |
|
|
|
|
| 485 |
program and the same input. However, if any such execution contains an
|
| 486 |
undefined operation, this International Standard places no requirement
|
| 487 |
on the implementation executing that program with that input (not even
|
| 488 |
with regard to operations preceding the first undefined operation).
|
| 489 |
|
| 490 |
+
If a signal handler is executed as a result of a call to the `raise`
|
| 491 |
+
function, then the execution of the handler is sequenced after the
|
| 492 |
+
invocation of the `raise` function and before its return. When a signal
|
| 493 |
+
is received for another reason, the execution of the signal handler is
|
| 494 |
+
usually unsequenced with respect to the rest of the program.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
|
| 496 |
An instance of each object with automatic storage duration (
|
| 497 |
[[basic.stc.auto]]) is associated with each entry into its block. Such
|
| 498 |
an object exists and retains its last-stored value during the execution
|
| 499 |
of the block and while the block is suspended (by a call of a function
|
|
|
|
| 561 |
exception and in which the results of overflows are reversible, the
|
| 562 |
above expression statement can be rewritten by the implementation in any
|
| 563 |
of the above ways because the same result will occur.
|
| 564 |
|
| 565 |
A *full-expression* is an expression that is not a subexpression of
|
| 566 |
+
another expression. in some contexts, such as unevaluated operands, a
|
| 567 |
+
syntactic subexpression is considered a full-expression (Clause
|
| 568 |
+
[[expr]]). If a language construct is defined to produce an implicit
|
| 569 |
+
call of a function, a use of the language construct is considered to be
|
| 570 |
+
an expression for the purposes of this definition. A call to a
|
| 571 |
+
destructor generated at the end of the lifetime of an object other than
|
| 572 |
+
a temporary object is an implicit full-expression. Conversions applied
|
| 573 |
+
to the result of an expression in order to satisfy the requirements of
|
| 574 |
+
the language construct in which the expression appears are also
|
| 575 |
+
considered to be part of the full-expression.
|
| 576 |
|
| 577 |
``` cpp
|
| 578 |
struct S {
|
| 579 |
S(int i): I(i) { }
|
| 580 |
int& v() { return I; }
|
|
|
|
| 625 |
before *A*, but it is unspecified which. Indeterminately sequenced
|
| 626 |
evaluations cannot overlap, but either could be executed first.
|
| 627 |
|
| 628 |
Every value computation and side effect associated with a
|
| 629 |
full-expression is sequenced before every value computation and side
|
| 630 |
+
effect associated with the next full-expression to be evaluated.[^8]
|
| 631 |
|
| 632 |
Except where noted, evaluations of operands of individual operators and
|
| 633 |
of subexpressions of individual expressions are unsequenced. In an
|
| 634 |
expression that is evaluated more than once during the execution of a
|
| 635 |
program, unsequenced and indeterminately sequenced evaluations of its
|
| 636 |
subexpressions need not be performed consistently in different
|
| 637 |
evaluations. The value computations of the operands of an operator are
|
| 638 |
sequenced before the value computation of the result of the operator. If
|
| 639 |
a side effect on a scalar object is unsequenced relative to either
|
| 640 |
another side effect on the same scalar object or a value computation
|
| 641 |
+
using the value of the same scalar object, and they are not potentially
|
| 642 |
+
concurrent ([[intro.multithread]]), the behavior is undefined. The next
|
| 643 |
+
section imposes similar, but more complex restrictions on potentially
|
| 644 |
+
concurrent computations.
|
| 645 |
|
| 646 |
``` cpp
|
| 647 |
void f(int, int);
|
| 648 |
void g(int i, int* v) {
|
| 649 |
i = v[i++]; // the behavior is undefined
|
|
|
|
| 693 |
some kinds of atomic operations, for example, allow executions
|
| 694 |
inconsistent with a simple interleaving, as described below. Under a
|
| 695 |
freestanding implementation, it is *implementation-defined* whether a
|
| 696 |
program can have more than one thread of execution.
|
| 697 |
|
| 698 |
+
A signal handler that is executed as a result of a call to the `raise`
|
| 699 |
+
function belongs to the same thread of execution as the call to the
|
| 700 |
+
`raise` function. Otherwise it is unspecified which thread of execution
|
| 701 |
+
contains a signal handler invocation.
|
| 702 |
+
|
| 703 |
Implementations should ensure that all unblocked threads eventually make
|
| 704 |
progress. Standard library functions may silently block on I/O or locks.
|
| 705 |
Factors in the execution environment, including externally-imposed
|
| 706 |
thread priorities, may prevent an implementation from making certain
|
| 707 |
guarantees of forward progress.
|
| 708 |
|
| 709 |
+
Executions of atomic functions that are either defined to be lock-free (
|
| 710 |
+
[[atomics.flag]]) or indicated as lock-free ([[atomics.lockfree]]) are
|
| 711 |
+
*lock-free executions*.
|
| 712 |
+
|
| 713 |
+
- If there is only one unblocked thread, a lock-free execution in that
|
| 714 |
+
thread shall complete. Concurrently executing threads may prevent
|
| 715 |
+
progress of a lock-free execution. For example, this situation can
|
| 716 |
+
occur with load-locked store-conditional implementations. This
|
| 717 |
+
property is sometimes termed obstruction-free.
|
| 718 |
+
- When one or more lock-free executions run concurrently, at least one
|
| 719 |
+
should complete. It is difficult for some implementations to provide
|
| 720 |
+
absolute guarantees to this effect, since repeated and particularly
|
| 721 |
+
inopportune interference from other threads may prevent forward
|
| 722 |
+
progress, e.g., by repeatedly stealing a cache line for unrelated
|
| 723 |
+
purposes between load-locked and store-conditional instructions.
|
| 724 |
+
Implementations should ensure that such effects cannot indefinitely
|
| 725 |
+
delay progress under expected operating conditions, and that such
|
| 726 |
+
anomalies can therefore safely be ignored by programmers. Outside this
|
| 727 |
+
International Standard, this property is sometimes termed lock-free.
|
| 728 |
+
|
| 729 |
The value of an object visible to a thread *T* at a particular point is
|
| 730 |
the initial value of the object, a value assigned to the object by *T*,
|
| 731 |
or a value assigned to the object by another thread, according to the
|
| 732 |
rules below. In some cases, there may instead be undefined behavior.
|
| 733 |
Much of this section is motivated by the desire to support atomic
|
|
|
|
| 877 |
visibly reordered. This is not actually detectable without data races,
|
| 878 |
but it is necessary to ensure that data races, as defined below, and
|
| 879 |
with suitable restrictions on the use of atomics, correspond to data
|
| 880 |
races in a simple interleaved (sequentially consistent) execution.
|
| 881 |
|
| 882 |
+
The value of an atomic object *M*, as determined by evaluation *B*,
|
| 883 |
+
shall be the value stored by some side effect *A* that modifies *M*,
|
| 884 |
+
where *B* does not happen before *A*. The set of such side effects is
|
| 885 |
+
also restricted by the rest of the rules described here, and in
|
| 886 |
+
particular, by the coherence requirements below.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 887 |
|
| 888 |
If an operation *A* that modifies an atomic object *M* happens before an
|
| 889 |
operation *B* that modifies *M*, then *A* shall be earlier than *B* in
|
| 890 |
the modification order of *M*. This requirement is known as write-write
|
| 891 |
coherence.
|
|
|
|
| 896 |
stored by *X* or the value stored by a side effect *Y* on *M*, where *Y*
|
| 897 |
follows *X* in the modification order of *M*. This requirement is known
|
| 898 |
as read-read coherence.
|
| 899 |
|
| 900 |
If a value computation *A* of an atomic object *M* happens before an
|
| 901 |
+
operation *B* that modifies *M*, then *A* shall take its value from a
|
| 902 |
+
side effect *X* on *M*, where *X* precedes *B* in the modification order
|
| 903 |
+
of *M*. This requirement is known as read-write coherence.
|
| 904 |
|
| 905 |
If a side effect *X* on an atomic object *M* happens before a value
|
| 906 |
computation *B* of *M*, then the evaluation *B* shall take its value
|
| 907 |
from *X* or from a side effect *Y* that follows *X* in the modification
|
| 908 |
order of *M*. This requirement is known as write-read coherence.
|
|
|
|
| 910 |
The four preceding coherence requirements effectively disallow compiler
|
| 911 |
reordering of atomic operations to a single object, even if both
|
| 912 |
operations are relaxed loads. This effectively makes the cache coherence
|
| 913 |
guarantee provided by most hardware available to C++atomic operations.
|
| 914 |
|
| 915 |
+
The value observed by a load of an atomic depends on the “happens
|
| 916 |
+
before” relation, which depends on the values observed by loads of
|
| 917 |
+
atomics. The intended reading is that there must exist an association of
|
| 918 |
+
atomic loads with modifications they observe that, together with
|
| 919 |
+
suitably chosen modification orders and the “happens before” relation
|
| 920 |
+
derived as described above, satisfy the resulting constraints as imposed
|
| 921 |
+
here.
|
| 922 |
+
|
| 923 |
+
Two actions are *potentially concurrent* if
|
| 924 |
+
|
| 925 |
+
- they are performed by different threads, or
|
| 926 |
+
- they are unsequenced, and at least one is performed by a signal
|
| 927 |
+
handler.
|
| 928 |
|
| 929 |
The execution of a program contains a *data race* if it contains two
|
| 930 |
+
potentially concurrent conflicting actions, at least one of which is not
|
| 931 |
+
atomic, and neither happens before the other, except for the special
|
| 932 |
+
case for signal handlers described below. Any such data race results in
|
| 933 |
+
undefined behavior. It can be shown that programs that correctly use
|
| 934 |
mutexes and `memory_order_seq_cst` operations to prevent all data races
|
| 935 |
and use no other synchronization operations behave as if the operations
|
| 936 |
executed by their constituent threads were simply interleaved, with each
|
| 937 |
value computation of an object being taken from the last side effect on
|
| 938 |
that object in that interleaving. This is normally referred to as
|
|
|
|
| 941 |
transformations that do not change single-threaded program semantics. In
|
| 942 |
fact, most single-threaded program transformations continue to be
|
| 943 |
allowed, since any program that behaves differently as a result must
|
| 944 |
perform an undefined operation.
|
| 945 |
|
| 946 |
+
Two accesses to the same object of type `volatile sig_atomic_t` do not
|
| 947 |
+
result in a data race if both occur in the same thread, even if one or
|
| 948 |
+
more occurs in a signal handler. For each signal handler invocation,
|
| 949 |
+
evaluations performed by the thread invoking a signal handler can be
|
| 950 |
+
divided into two groups *A* and *B*, such that no evaluations in *B*
|
| 951 |
+
happen before evaluations in *A*, and the evaluations of such
|
| 952 |
+
`volatile sig_atomic_t` objects take values as though all evaluations in
|
| 953 |
+
*A* happened before the execution of the signal handler and the
|
| 954 |
+
execution of the signal handler happened before all evaluations in *B*.
|
| 955 |
+
|
| 956 |
Compiler transformations that introduce assignments to a potentially
|
| 957 |
shared memory location that would not be modified by the abstract
|
| 958 |
machine are generally precluded by this standard, since such an
|
| 959 |
assignment might overwrite another assignment by a different thread in
|
| 960 |
cases in which an abstract machine execution would not have encountered
|
| 961 |
a data race. This includes implementations of data member assignment
|
| 962 |
that overwrite adjacent members in separate memory locations. Reordering
|
| 963 |
of atomic loads in cases in which the atomics in question may alias is
|
| 964 |
+
also generally precluded, since this may violate the coherence rules.
|
|
|
|
| 965 |
|
| 966 |
Transformations that introduce a speculative read of a potentially
|
| 967 |
shared memory location may not preserve the semantics of the C++program
|
| 968 |
as defined in this standard, since they potentially introduce a data
|
| 969 |
race. However, they are typically valid in the context of an optimizing
|
|
|
|
| 1007 |
|
| 1008 |
All rights in these originals are reserved.
|
| 1009 |
|
| 1010 |
<!-- Link reference definitions -->
|
| 1011 |
[atomics]: atomics.md#atomics
|
| 1012 |
+
[atomics.flag]: atomics.md#atomics.flag
|
| 1013 |
[atomics.lockfree]: atomics.md#atomics.lockfree
|
| 1014 |
[atomics.order]: atomics.md#atomics.order
|
| 1015 |
[basic]: basic.md#basic
|
| 1016 |
[basic.compound]: basic.md#basic.compound
|
| 1017 |
[basic.def]: basic.md#basic.def
|