Execute a given function exactly once in a multi-threaded environment or wait for the function to be executed by another thread.
More...
Execute a given function exactly once in a multi-threaded environment or wait for the function to be executed by another thread.
Example usage: First the user must create a control variable of type os_once::state_t and assign it os_once::NEVER_DONE. Then the user must pass this variable, together with a function to be executed to os_once::do_or_wait_for_done().
Multiple threads can call os_once::do_or_wait_for_done() simultaneously with the same (os_once::state_t) control variable. The provided function will be called exactly once and when os_once::do_or_wait_for_done() returns then this function has completed execution, by this or another thread. In other words os_once::do_or_wait_for_done() will either execute the provided function or will wait for its execution to complete if it is already called by another thread or will do nothing if the function has already completed its execution earlier.
This mimics pthread_once(3), but unfortunately pthread_once(3) does not support passing arguments to the init_routine() function. We should use std::call_once() when we start compiling with C++11 enabled.