Reference: IVEThreadCallOnce

Overview

When multiple threads call IVEThreadCallOnce with a pointer to the same IVEThreadOnceFlag variable, it is guaranteed that the initialization function registered with IVEThreadCallOnce will be invoked once and that none of the threads will return until after the initialization function has been completed. This is useful for lazy initialization of something which may be used by multiple threads.

Prototype (C)

#include "ive_standards.h"
#include "ive_thread.h"
void IVEThreadCallOnce(void (*init_routine)(void), IVEThreadOnceFlag* p_once);

Prototype (Fortran)

There is no Fortran interface.

Parameters

init_routine
The procedure pointed to by init_routine is only called when multiple threads call IVEThreadCallOnce with the same value for p_once.
p_once
p_once points to the initialization variable. To ensure that init_routine is called exactly once, all calls to IVEThreadCallOnce with init_routine should have p_once point to the same initialization variable.

Example

The code fragment below guarantees that initialize_lut() is called only once no matter how many timers get_lut() is called.

#include "ive_standards.h"
#include "ive_thread.h"
static void initialize_lut(void);
static float* g_lut;

float*
get_lut(void)
{
    static IVEThreadOnceFlag flag = IVE_ONCE_INIT;

    IVEThreadCallOnce(initialize_lut, &flag);
    return g_lut;
}

static
void
initialize_lut(void)
{
    /*
     * Initialize g_lut here.
     */
}

Side effects

Inclusion of ive_standards.h sets certain feature-set macros. It is safest to include it before any other headers to avoid inconsistent definitions.

Cross references

IVEThreadOnceFlag


| categories | alphabetical |


modified May 7, 2002

Eric Branlund (eric@msg.ucsf.edu)