CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 31 Jul 2025 12:23:09 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/mutex/call_once/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 31 Jul 2025 12:23:09 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"78b6-qP01rpAEXCy1PoDdGPo+deUR2Co"
Content-Encoding: gzip
Calls fn passing args as arguments, unless another thread has already executed (or is currently executing) a call to call_once with the same flag.
If another thread is already actively executing a call to call_once with the same flag, it causes a passive execution: Passive executions do not call fn but do not return until the active execution itself has returned, and all visible side effects are synchronized at that point among all concurrent calls to this function with the same flag.
If an active call to call_once ends by throwing an exception (which is propagated to its calling thread) and passive executions exist, one is selected among these passive executions, and called to be the new active call instead.
Note that once an active execution has returned, all current passive executions and future calls to call_once (with the same flag) also return without becoming active executions.
The active execution uses decay copies of the lvalue or rvalue references of fn and args, ignoring the value returned by fn.
Possible output (winner may vary):
Otherwise, active executions provide the same level of guarantees as the operation performed on the arguments.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<mutex>
classes
-
adopt_lock_tC++11
-
defer_lock_tC++11
-
lock_guardC++11
-
mutexC++11
-
once_flagC++11
-
recursive_mutexC++11
-
recursive_timed_mutexC++11
-
timed_mutexC++11
-
try_to_lock_tC++11
-
unique_lockC++11
-
functions
constants
-
adopt_lockC++11
-
defer_lockC++11
-
try_to_lockC++11
-
public member function
<mutex>
std::call_once
template <class Fn, class... Args> void call_once (once_flag& flag, Fn&& fn, Args&&... args);
Call function once
If another thread is already actively executing a call to call_once with the same flag, it causes a passive execution: Passive executions do not call fn but do not return until the active execution itself has returned, and all visible side effects are synchronized at that point among all concurrent calls to this function with the same flag.
If an active call to call_once ends by throwing an exception (which is propagated to its calling thread) and passive executions exist, one is selected among these passive executions, and called to be the new active call instead.
Note that once an active execution has returned, all current passive executions and future calls to call_once (with the same flag) also return without becoming active executions.
The active execution uses decay copies of the lvalue or rvalue references of fn and args, ignoring the value returned by fn.
Parameters
- flag
-
Object used by the function to track the state of invocations.
Using the same object for calls in different threads, results in a single call if called concurrently.
If flag has a state that is not valid, the function throws a system_error exception with an invalid_argument error condition.
If flag has a state that is not valid, the call causes undefined behavior.
<mutex>
to be used as argument to this function.
- fn
- A pointer to function, pointer to member, or any kind of move-constructible function object (i.e., an object whose class defines operator(), including closures and function objects).
The return value (if any) is ignored.
- args...
- Arguments passed to the call to fn. Their types shall be move-constructible.
If fn is a member pointer, the first argument shall be an object for which that member is defined (or a reference, or a pointer to it).
Return Value
noneExample
|
|
Possible output (winner may vary):
waiting for the first among 10 threads to count 1000 ms... winner thread: 2 |
Data races
The function modifies flag, and accesses fn and args to create decay copies of their lvalue or rvalue references.Exception safety
If the function itself fails, it throws a system_error exception, leaving all objects in a valid state (basic guarantee).Otherwise, active executions provide the same level of guarantees as the operation performed on the arguments.
See also
- once_flag
- Flag argument type for call_once (class)
- thread
- Thread (class)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us