LazyInit Class Reference 线程

#include <lazyinit.h>

详细描述

Thread-safe helper class for single-threaded lazy initialization.

Typical usage case is a method which initializes data on first call, for example

class Sample { public : MyObject* GetObject() { _state .Init( [ this ]() -> Bool { // Your init code goes here. // Return true if initialization was successful and false if it failed. // Using type Result<void> instead of Bool for the return value is supported as well. return true ; });
return _object; } private : LazyInit _state ; MyObject* _object; SomeMoreData _xyz; };
注意
The code of the lambda must be single threaded, using multithreaded code will lead to a deadlock! For potentially multithreaded initialization use LazyInitThreaded . Performance note: Declaring a global LazyInit as a static member of a class will degrade its performance because the compiler will guard its access with a slow and unnecessary mutex. To avoid this move the global state outside of the class.

THREADSAFE.

公共成员函数

template<typename FN >
MAXON_ATTRIBUTE_FORCE_INLINE auto  Init (FN &&fn) -> decltype(fn())
template<typename FN = decltype(Dummy)>
void  重置 (FN &&fn= Dummy )
Bool   IsInitialized () const
  operator Bool () const

Private Types

enum   STATE : Int32 {
   UNINITIALIZED ,
   PENDING ,
   INITIALIZED
}

私有成员函数

enum maxon::LazyInit::STATE Int32   MAXON_ENUM_LIST_CLASS ( STATE )

Static Private Member Functions

static void  Dummy ()
static Bool   ToBool ( Bool v)
static Bool   ToBool ( ResultMem v)
static Bool   ToBool (const Result < void > &v)

Private Attributes

AtomicInt32   _state

Member Enumeration Documentation

◆  STATE

enum STATE : Int32
strong private
Enumerator
UNINITIALIZED 
PENDING 
INITIALIZED 

成员函数文档编制

◆  MAXON_ENUM_LIST_CLASS()

enum maxon::LazyInit::STATE Int32 MAXON_ENUM_LIST_CLASS ( STATE   )
private

◆  Dummy()

static void Dummy ( )
static private

◆  ToBool() [1/3]

static Bool ToBool ( Bool   v )
static private

◆  ToBool() [2/3]

static Bool ToBool ( ResultMem   v )
static private

◆  ToBool() [3/3]

static Bool ToBool ( const Result < void > &  v )
static private

◆  Init()

MAXON_ATTRIBUTE_FORCE_INLINE auto Init ( FN &&  fn ) -> decltype(fn())

Initializes an object by calling the specified method (and does nothing if the object has already been initialized). The method #fn will be executed on the current thread and must be executing quickly. It is not allowed to perform multithreaded code as part of the initialization because this could deadlock and waiting threads would be busy-idling.

注意
The code of the lambda must be single threaded! Otherwise use LazyInitThreaded (and review your code thoroughly). THREADSAFE.
参数
[in] fn Method (usually a lambda) to initialize something, must return a Bool or a Result<void> .
返回
True/OK if initialization was successful or object has already been initialized, otherwise result of failed initialization.

◆  Reset()

void Reset ( FN &&  fn = Dummy )

Resets an object by calling the specified method. Does nothing if the object has already been reset. THREADSAFE.

参数
[in] fn Optional method (usually a lambda) to reset something.

◆  IsInitialized()

Bool IsInitialized ( ) const

Returns if the object already has been initialized. THREADSAFE.

返回
True if initialization was successful.

◆  operator Bool()

operator Bool ( ) const
explicit

Returns if the object already has been initialized. THREADSAFE.

返回
True if initialization was successful.

Member Data Documentation

◆  _state

AtomicInt32 _state
private
maxon::LazyInit::_state
AtomicInt32 _state
定义: lazyinit.h:165
Bool
maxon::Bool Bool
定义: ge_sys_math.h:53