Thursday, April 25, 2013

pthread_once example c c++


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.  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 rou-
     tine 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'').

Example

See Code disclaimer information for information pertaining to code examples.
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

#define         NUMTHREADS    3
int             number      = 0;
int             okStatus    = 777;
pthread_once_t  onceControl = PTHREAD_ONCE_INIT;

void initRoutine(void)
{
  printf("In the initRoutine\n");
  number++;
}

void *threadfunc(void *parm)
{
  printf("Inside secondary thread\n");
  pthread_once(&onceControl, initRoutine);
  return __VOID(okStatus);
}

int main(int argc, char **argv)
{
  pthread_t             thread[NUMTHREADS];
  int                   rc=0;
  int                   i=NUMTHREADS;
  void                 *status;

  printf("Enter Testcase - %s\n", argv[0]);

  for (i=0; i < NUMTHREADS; ++i) {
    printf("Create thread %d\n",
           i);
    rc = pthread_create(&thread[i], NULL, threadfunc, NULL);
    checkResults("pthread_create()\n", rc);
  }


  for (i=0; i < NUMTHREADS; ++i) {
    printf("Wait for thread %d\n", i);
    rc = pthread_join(thread[i], &status);
    checkResults("pthread_join()\n", rc);
    if (__INT(status) != okStatus) {
      printf("Secondary thread failed\n");
      exit(1);
    }
  }

  if (number != 1) {
    printf("An incorrect number of 1 one-time init routine was called!\n");
    exit(1);
  }
  printf("One-time init routine called exactly once\n");
  printf("Main completed\n");
  return 0;
}
Output:
Enter Testcase - QP0WTEST/TPONCE0
Create thread 0
Create thread 1
Create thread 2
Wait for thread 0
Inside secondary thread
In the initRoutine
Inside secondary thread
Wait for thread 1
Wait for thread 2
Inside secondary thread
One-time init routine called exactly once
Main completed