Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

run_once(9) [netbsd man page]

RUN_ONCE(9)						   BSD Kernel Developer's Manual					       RUN_ONCE(9)

NAME
RUN_ONCE -- Run a function exactly once SYNOPSIS
#include <sys/once.h> ONCE_DECL(control); int RUN_ONCE(once_t *control, int (*init_func)(void)); DESCRIPTION
RUN_ONCE() provides a functionality similar to pthread_once(3). It ensures that, for a given control, init_func() is executed (successfully) exactly once. It is considered as a successful execution if and only if init_func() returned 0. As long as there was no successful execu- tion, RUN_ONCE() will try again each time it is called. RUN_ONCE() can sleep if it's called concurrently. RETURN VALUES
On failure, RUN_ONCE() returns what init_func() returned. Otherwise, it returns 0. EXAMPLES
The following example shows how RUN_ONCE() is used. Regardless of how many times some_func() is executed, init_func() will be executed exactly once. static int init_func(void) { /* * do some initialization. */ return 0; /* success */ } int some_func(void) { static ONCE_DECL(control); RUN_ONCE(&control, init_func); /* * we are sure that init_func has already been completed here. */ } SEE ALSO
pthread_once(3), condvar(9), intro(9) BSD
July 7, 2010 BSD

Check Out this Related Man Page

PTHREAD_ONCE(3) 					   BSD Library Functions Manual 					   PTHREAD_ONCE(3)

NAME
pthread_once -- dynamic package initialization SYNOPSIS
#include <pthread.h> pthread_once_t once_control = PTHREAD_ONCE_INIT; int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); DESCRIPTION
The first call to pthread_once() by any thread in a process, with a given once_control, will call the init_routine() with no arguments. Sub- sequent calls to pthread_once() with the same once_control will not call the init_routine(). On return from pthread_once(), it is guaranteed that init_routine() has completed. The once_control parameter is used to determine whether the associated initialization routine has been called. The function pthread_once() is not a cancellation point. However, if init_routine() is a cancellation point and is cancelled, the effect on once_control is as if pthread_once() was never called. The constant PTHREAD_ONCE_INIT is defined by header <pthread.h>. The behavior of pthread_once() is undefined if once_control has automatic storage duration or is not initialized by PTHREAD_ONCE_INIT. RETURN VALUES
If successful, the pthread_once() function will return zero. Otherwise, an error number will be returned to indicate the error. ERRORS
None. STANDARDS
pthread_once() conforms to ISO/IEC 9945-1:1996 (``POSIX.1''). BSD
April 4, 1996 BSD
Man Page