Mstdlib-1.24.0
Threadsafe initialization helpers (Thread Once)

Data Structures

struct  M_thread_once_t
 

Macros

#define M_THREAD_ONCE_STATIC_INITIALIZER   { M_FALSE, M_THREAD_SPINLOCK_STATIC_INITIALIZER }
 

Functions

M_bool M_thread_once (M_thread_once_t *once_control, void(*init_routine)(M_uint64 flags), M_uint64 init_flags)
 
M_bool M_thread_once_reset (M_thread_once_t *once_control)
 

Detailed Description

Threadsafe initialization helpers (Thread Once)


Data Structure Documentation

◆ M_thread_once_t

struct M_thread_once_t

Public struct for M_thread_once, so static initializer can be used

Data Fields
M_bool initialized
M_thread_spinlock_t spinlock

Macro Definition Documentation

◆ M_THREAD_ONCE_STATIC_INITIALIZER

#define M_THREAD_ONCE_STATIC_INITIALIZER   { M_FALSE, M_THREAD_SPINLOCK_STATIC_INITIALIZER }

Static initializer for M_thread_once

Function Documentation

◆ M_thread_once()

M_bool M_thread_once ( M_thread_once_t once_control,
void(*)(M_uint64 flags)  init_routine,
M_uint64  init_flags 
)

Ensure an initialization routine is performed only once, even if called from multiple threads simultaneously.

Performing initialization in a multi-threaded program can cause race conditions.

Take this code example: static int initialized = 0; if (!initialized) { init_routine(); initialized = 1; }

If two threads where to enter this simultaneously, before init_routine() was complete, they would call it twice. The above code example can be replaced with:

static M_thread_once_t initialized = M_THREAD_ONCE_STATIC_INITIALIZER; M_thread_once(&initialized, init_routine);

Parameters
[in]once_controlOnce control variable passed by reference, and first set to M_THREAD_ONCE_STATIC_INITIALIZER;
[in]init_routineInitialization routine to be called if it has not yet been called.
[in]init_flagsFlags to be passed onto the initialization routine.
Returns
M_TRUE if init routine was just run, M_FALSE if not run (previously run)

◆ M_thread_once_reset()

M_bool M_thread_once_reset ( M_thread_once_t once_control)

Reset the once_control object back to an uninitialized state. Useful to be called in a destructor so an initialization routine can be re-run.

Parameters
[in]once_controlOnce control variable passed by reference, and first set to M_THREAD_ONCE_STATIC_INITIALIZER;
Returns
M_TRUE if reset, M_FALSE if not initialized.