std::call_once
| Defined in header <mutex>
|
||
| template< class Callable, class... Args > void call_once( std::once_flag& flag, Callable&& f, Args&&... args ); |
(since C++11) | |
Executes the Callable object f exactly once, even if called from several threads.
Each group of call_once invocations that receives the same std::once_flag object will meet the following requirements:
- Exactly one execution of exactly one of the functions (passed as
fto the invocations in the group) is performed. It is undefined which function will be selected for execution. The selected function runs in the same thread as thecall_onceinvocation it was passed to.
- No invocation in the group returns before the above-mentioned execution of the selected function is completed successfully, that is, doesn't exit via an exception.
- If the selected function exits via exception, it is propagated to the caller. Another function is then selected and executed.
Parameters
| flag | - | an object, for which exactly one function gets executed |
| f | - | Callable object to invoke
|
| args... | - | arguments to pass to the function |
Return value
(none)
Exceptions
- std::system_error if any condition prevents calls to
call_oncefrom executing as specified - any exception thrown by
f
Notes
|
The arguments to the |
(until C++17) |
|
The arguments to the |
(since C++17) |
Initialization of function-local statics is guaranteed to occur only once even when called from multiple threads, and may be more efficient than the equivalent code using std::call_once.