LazyInitThreaded Class Reference 线程

#include <lazyinitthreaded.h>

详细描述

Helper class for multithreaded lazy initialization.

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

class Sample { public : Result<MyObject*> GetObject() { iferr_scope ;
_state .Init( [ this ]() -> Result<void> { // Invoke complex multithreaded initialization code. return ThreadedInitialization(); }); return _object; } private : LazyInitThreaded _state ; MyObject* _object; SomeMoreData _xyz; };

Performance note: Declaring a global LazyInitThreaded 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.

By default cancellation of the caller will be forwarded to the lambda passed to Init() and after a ThreadCancelledError Init() will retry the initialization.

注意
要使用 LazyInitThreaded you have to include "job.h" too.

THREADSAFE.

Classes

class   CancellationForwarder

Public Types

enum   OPTIONS : Int {
   NONE ,
   FORWARD_CANCELLATION ,
   RETRY_ON_FAILURE ,
   RETRY_ON_CANCELLATION ,
   DEFAULT
}

公共成员函数

enum maxon::LazyInitThreaded::OPTIONS Int   MAXON_ENUM_FLAGS_CLASS ( OPTIONS )
  ~LazyInitThreaded ()
template<typename FN >
MAXON_ATTRIBUTE_FORCE_INLINE auto  Init (FN &&fn, OPTIONS options= OPTIONS::DEFAULT ) -> decltype(fn())
template<typename FN = decltype(Dummy)>
void  重置 (FN &&fn= Dummy )
Bool   IsInitialized () const
Bool   HasFailed () const
  operator Bool () const

Static Private Member Functions

static void  Dummy ()

Private Attributes

maxon::LazyInitThreaded::CancellationForwarder   MAXON_ENUM_FLAGS_CLASS
AtomicInt   _state

Static Private Attributes

static const Int   UNINITIALIZED
static const Int   FAILED
static const Int   PENDING
static const Int   ENQUEUED
static const Int   CHECKING
static const Int   INITIALIZED
static const Int   STATE_MASK
static const Int   VALUE_MASK

Member Enumeration Documentation

◆  OPTIONS

enum OPTIONS : Int
strong
Enumerator
NONE 
FORWARD_CANCELLATION 

Cancellation of the calling job will be forwarded.

RETRY_ON_FAILURE 

Retry initialization if it failed the first time.

RETRY_ON_CANCELLATION 

Retry initialization if it failed due to thread cancellation (ThreadCancelledError). This is a special case of RETRY_ON_FAILURE.

DEFAULT 

Default behaviour: Cancellation of the calling job will be forwarded, initialization will be retried if it failed due to ThreadCancelledError.

构造函数 & 析构函数文档编制

◆  ~LazyInitThreaded()

~ LazyInitThreaded ( )

成员函数文档编制

◆  Dummy()

static void Dummy ( )
static private

◆  MAXON_ENUM_FLAGS_CLASS()

enum maxon::LazyInitThreaded::OPTIONS Int MAXON_ENUM_FLAGS_CLASS ( OPTIONS   )

◆  Init()

MAXON_ATTRIBUTE_FORCE_INLINE auto Init ( FN &&  fn ,
OPTIONS   选项 = OPTIONS::DEFAULT  
) -> decltype(fn())

Initializes an object by calling the specified method (and does nothing if the object has already been initialized). The method #fnwill be executed by a job and is free to execute long running multithreaded code for initialization. Waiting threads will either participate in the initialization or go to sleep. They will not idle unnecessarily. If the initialization failed once and #retryOnFailure was false (the default) all following Init() calls will return the initial error. THREADSAFE.

参数
[in] fn Method (usually a lambda) to initialize something, must return Result<void> .
[in] 选项 OPTIONS::FORWARD_CANCELLATION by default.
返回
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. If the initialization failed the optional reset method won't be invoked. 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.

◆  HasFailed()

Bool HasFailed ( ) const

◆  operator Bool()

operator Bool ( ) const
explicit

Returns if the object already has been initialized. THREADSAFE.

返回
True if initialization was successful.

Member Data Documentation

◆  UNINITIALIZED

const Int UNINITIALIZED
static private

◆  FAILED

const Int FAILED
static private

◆  PENDING

const Int PENDING
static private

◆  ENQUEUED

const Int ENQUEUED
static private

◆  CHECKING

const Int CHECKING
static private

◆  INITIALIZED

const Int INITIALIZED
static private

◆  STATE_MASK

const Int STATE_MASK
static private

◆  VALUE_MASK

const Int VALUE_MASK
static private

◆  MAXON_ENUM_FLAGS_CLASS

maxon::LazyInitThreaded::CancellationForwarder MAXON_ENUM_FLAGS_CLASS
private

◆  _state

AtomicInt _state
private
maxon::LazyInitThreaded::_state
AtomicInt _state
定义: lazyinitthreaded.h:364
iferr_scope
#define iferr_scope
定义: resultbase.h:1343