std::call_once
is used with an instance of std::once_flag to ensure that a particular
function is called exactly once, even if multiple threads invoke the call
concurrently.
template<typename Callable,typename... Args> void call_once(std::once_flag& flag,Callable func,Args args...);
- Preconditions:
The expression INVOKE(
func,args) is valid for the supplied values offuncandargs.Callableand every member ofArgsisMoveConstructible.- Effects:
Invocations of
std::call_onceon the samestd::once_flagobject are serialized. If there has been no prior effectivestd::call_onceinvocation on the samestd::once_flagobject, the argumentfunc(or a copy thereof) is called as-if by INVOKE(func,args), and the invocation ofstd::call_onceis effective if and only if the invocation offuncreturns without throwing an exception. If an exception is thrown, the exception is propagated to the caller. If there has been a prior effectivestd::call_onceon the samestd::once_flagobject, the invocation ofstd::call_oncereturns without invokingfunc.- Synchronization:
The completion of an effective
std::call_onceinvocation on astd::once_flagobject, happens-before all subsequentstd::call_onceinvocations on the samestd::once_flagobject.- Throws:
std::system_errorwhen the effects cannot be achieved, or any exception propagated from the invocation offunc.- Note:
On compilers that don't support variadic templates, an overloaded set of function templates is provided that supports passing of up to 5 arguments.
Header
#include <mutex>