The std::once_flag
default constructor creates a new std::once_flag instance in a state
which indicates that the associated function has not been called.
constexpr once_flag();
- Effects:
Construct a new
std::once_flaginstance in a state which indicates that the associated function has not been called. Since this is aconstexprconstructor, an instance with static storage duration is constructed as part of the static initialization phase, which avoids race conditions and order-of-initialization problems.
just::thread Specific
With Microsoft Visual Studio 2008, initialization for local static objects with constructors is always dynamic initialization, and initialization of global objects with constructors is only static initialization if the optimization level is high enough.
Therefore, the macro _JSS_STATIC_ONCE_FLAG
is provided in order to guarantee static initialization.
// only static initialization at /O1 or higher std::once_flag global; // guaranteed static initialization _JSS_STATIC_ONCE_FLAG(global2); void foo() { // should be static initialization, // but isn't guaranteed static std::once_flag local; // guaranteed static initialization _JSS_STATIC_ONCE_FLAG(local2); }
If a namespace scope instance is referenced from another translation
unit, use the _JSS_EXTERN_ONCE_FLAG
macro:
_JSS_EXTERN_ONCE_FLAG(global2); void bar() { std::call_once(global2,some_function); }
Header
#include <mutex>