Mstdlib-1.24.0
|
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) |
Threadsafe initialization helpers (Thread Once)
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 |
#define M_THREAD_ONCE_STATIC_INITIALIZER { M_FALSE, M_THREAD_SPINLOCK_STATIC_INITIALIZER } |
Static initializer for 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);
[in] | once_control | Once control variable passed by reference, and first set to M_THREAD_ONCE_STATIC_INITIALIZER; |
[in] | init_routine | Initialization routine to be called if it has not yet been called. |
[in] | init_flags | Flags to be passed onto the initialization routine. |
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.
[in] | once_control | Once control variable passed by reference, and first set to M_THREAD_ONCE_STATIC_INITIALIZER; |