tmp/tmpbn1c7f5j/{from.md → to.md}
RENAMED
|
@@ -1,61 +1,77 @@
|
|
| 1 |
### Call once <a id="thread.once">[[thread.once]]</a>
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
The class `once_flag` is an opaque data structure that `call_once` uses
|
| 4 |
to initialize data without causing a data race or deadlock.
|
| 5 |
|
| 6 |
-
#### Struct `once_flag` <a id="thread.once.onceflag">[[thread.once.onceflag]]</a>
|
| 7 |
-
|
| 8 |
``` cpp
|
| 9 |
constexpr once_flag() noexcept;
|
| 10 |
```
|
| 11 |
|
| 12 |
*Effects:* Constructs an object of type `once_flag`.
|
| 13 |
|
| 14 |
*Synchronization:* The construction of a `once_flag` object is not
|
| 15 |
synchronized.
|
| 16 |
|
| 17 |
-
The object’s internal state is set to indicate to an
|
| 18 |
-
`call_once` with the object as its initial argument that
|
| 19 |
-
been called.
|
| 20 |
|
| 21 |
#### Function `call_once` <a id="thread.once.callonce">[[thread.once.callonce]]</a>
|
| 22 |
|
| 23 |
``` cpp
|
| 24 |
template<class Callable, class... Args>
|
| 25 |
void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
| 26 |
```
|
| 27 |
|
| 28 |
-
*Requires:*
|
| 29 |
-
|
| 30 |
-
`
|
| 31 |
-
(
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
*Effects:* An execution of `call_once` that does not call its `func` is
|
| 34 |
a *passive* execution. An execution of `call_once` that calls its `func`
|
| 35 |
-
is an *active* execution. An active execution shall call
|
| 36 |
-
|
| 37 |
-
`
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
execution.
|
| 47 |
|
| 48 |
*Synchronization:* For any given `once_flag`: all active executions
|
| 49 |
occur in a total order; completion of an active execution synchronizes
|
| 50 |
with ([[intro.multithread]]) the start of the next one in this total
|
| 51 |
order; and the returning execution synchronizes with the return from all
|
| 52 |
passive executions.
|
| 53 |
|
| 54 |
*Throws:* `system_error` when an exception is
|
| 55 |
required ([[thread.req.exception]]), or any exception thrown by `func`.
|
| 56 |
|
|
|
|
|
|
|
| 57 |
``` cpp
|
| 58 |
// global flag, regular function
|
| 59 |
void init();
|
| 60 |
std::once_flag flag;
|
| 61 |
|
|
@@ -80,5 +96,7 @@ class information {
|
|
| 80 |
public:
|
| 81 |
void verify() { std::call_once(verified, &information::verifier, *this); }
|
| 82 |
};
|
| 83 |
```
|
| 84 |
|
|
|
|
|
|
|
|
|
| 1 |
### Call once <a id="thread.once">[[thread.once]]</a>
|
| 2 |
|
| 3 |
+
#### Struct `once_flag` <a id="thread.once.onceflag">[[thread.once.onceflag]]</a>
|
| 4 |
+
|
| 5 |
+
``` cpp
|
| 6 |
+
namespace std {
|
| 7 |
+
struct once_flag {
|
| 8 |
+
constexpr once_flag() noexcept;
|
| 9 |
+
|
| 10 |
+
once_flag(const once_flag&) = delete;
|
| 11 |
+
once_flag& operator=(const once_flag&) = delete;
|
| 12 |
+
};
|
| 13 |
+
}
|
| 14 |
+
```
|
| 15 |
+
|
| 16 |
The class `once_flag` is an opaque data structure that `call_once` uses
|
| 17 |
to initialize data without causing a data race or deadlock.
|
| 18 |
|
|
|
|
|
|
|
| 19 |
``` cpp
|
| 20 |
constexpr once_flag() noexcept;
|
| 21 |
```
|
| 22 |
|
| 23 |
*Effects:* Constructs an object of type `once_flag`.
|
| 24 |
|
| 25 |
*Synchronization:* The construction of a `once_flag` object is not
|
| 26 |
synchronized.
|
| 27 |
|
| 28 |
+
*Postconditions:* The object’s internal state is set to indicate to an
|
| 29 |
+
invocation of `call_once` with the object as its initial argument that
|
| 30 |
+
no function has been called.
|
| 31 |
|
| 32 |
#### Function `call_once` <a id="thread.once.callonce">[[thread.once.callonce]]</a>
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
template<class Callable, class... Args>
|
| 36 |
void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
| 37 |
```
|
| 38 |
|
| 39 |
+
*Requires:*
|
| 40 |
+
|
| 41 |
+
``` cpp
|
| 42 |
+
INVOKE(std::forward<Callable>(func), std::forward<Args>(args)...)
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
(see [[func.require]]) shall be a valid expression.
|
| 46 |
|
| 47 |
*Effects:* An execution of `call_once` that does not call its `func` is
|
| 48 |
a *passive* execution. An execution of `call_once` that calls its `func`
|
| 49 |
+
is an *active* execution. An active execution shall call *INVOKE*(
|
| 50 |
+
std::forward\<Callable\>(func), std::forward\<Args\>(args)...). If such
|
| 51 |
+
a call to `func` throws an exception the execution is *exceptional*,
|
| 52 |
+
otherwise it is *returning*. An exceptional execution shall propagate
|
| 53 |
+
the exception to the caller of `call_once`. Among all executions of
|
| 54 |
+
`call_once` for any given `once_flag`: at most one shall be a returning
|
| 55 |
+
execution; if there is a returning execution, it shall be the last
|
| 56 |
+
active execution; and there are passive executions only if there is a
|
| 57 |
+
returning execution.
|
| 58 |
+
|
| 59 |
+
[*Note 1*: Passive executions allow other threads to reliably observe
|
| 60 |
+
the results produced by the earlier returning execution. — *end note*]
|
| 61 |
|
| 62 |
*Synchronization:* For any given `once_flag`: all active executions
|
| 63 |
occur in a total order; completion of an active execution synchronizes
|
| 64 |
with ([[intro.multithread]]) the start of the next one in this total
|
| 65 |
order; and the returning execution synchronizes with the return from all
|
| 66 |
passive executions.
|
| 67 |
|
| 68 |
*Throws:* `system_error` when an exception is
|
| 69 |
required ([[thread.req.exception]]), or any exception thrown by `func`.
|
| 70 |
|
| 71 |
+
[*Example 1*:
|
| 72 |
+
|
| 73 |
``` cpp
|
| 74 |
// global flag, regular function
|
| 75 |
void init();
|
| 76 |
std::once_flag flag;
|
| 77 |
|
|
|
|
| 96 |
public:
|
| 97 |
void verify() { std::call_once(verified, &information::verifier, *this); }
|
| 98 |
};
|
| 99 |
```
|
| 100 |
|
| 101 |
+
— *end example*]
|
| 102 |
+
|