Name

pthread_once
- dynamic package initialization

Library

libc_r.lib libpthread.lib libthr.lib

Synopsis

  #include <pthread.h>

pthread_once_t once_control = PTHREAD_ONCE_INIT;

  int pthread_once (pthread_once_t *once_control, void (*init_routine)(void));

Return values

If successful, the pthread_once function will return zero. Otherwise an error number will be returned to indicate the error.

Detailed 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. Subsequent 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 constant PTHREAD_ONCE_INIT is defined by header

  #include <pthread.h .>

The behavior of pthread_once is undefined if once_control has automatic storage duration or is not initialized by PTHREAD_ONCE_INIT.


Examples

/* The init function that pthread_once calls */
void an_init_func_1_1()
{
printf (" Initialized ..\n");
}
int XYZ()
{
 pthread_once_t once_control = PTHREAD_ONCE_INIT;
 /* Call pthread_once, passing it the once_control */
 pthread_once(&once_control, an_init_func_1_1);
 /* Call pthread_once again. The init function should not be
  * called. */
 pthread_once(&once_control, an_init_func_1_1);
}

         

Errors

The pthread_once function may fail if:
[EINVAL]
  If either once_control or init_routine is invalid.

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top