pthread_once function

http://blog.csdn.net/lmh12506/article/details/8452659

pthread_once is () function Detailed

In a multithreaded environment, some things need only be done once. Usually when the application is initialized, it can be relatively easily placed in the main function. But when you write a library, which can not be initialized in the main, you can use static initialization, but once initialized (pthread_once) would be more easier.
int pthread_once (pthread_once_t * once_control, void (* init_routine) (void));
Function: This function uses the initial value PTHREAD_ONCE_INIT once_control variable guaranteed init_routine () function is performed only once in the present process execution sequence.

In a multithreaded programming environment, although pthread_once () call will appear in multiple threads, init_routine () function is performed only once, where is a thread execution is uncertain, is determined by the kernel scheduler.

Linux Threads use mutexes and condition variables guaranteed by pthread_once () function performs a specified and performed only once, and once_control indicates whether executed. If once_control initial value is not PTHREAD_ONCE_INIT (Linux Threads defined as 0), pthread_once () will not normal behavior. In LinuxThreads, the actual "disposable function", there are three execution state: NEVER (0), IN_PROGRESS (1), DONE (2), if the initial value is set to 1 once, since all pthread_once is () must wait for one stimulate "executed a" signal, so all pthread_once () will fall into perpetual waiting; if set to 2, it indicates that the function has been performed once, so all pthread_once () will return 0 immediately.

Guess you like

Origin www.cnblogs.com/xingmuxin/p/11418379.html