PreviousNext

One-Time Initialization Routines

You probably have one or more routines that must be executed before any thread executes code in your application, but must be executed only once regardless of the sequence in which threads start executing. For example, you may want to create mutexes and condition variables (each of which must be created only once) in an initialization routine. Multiple threads can call the pthread_once( ) routine, or the pthread_once( ) routine can be called multiple times in the same thread, resulting in only one call to the specified routine.

Use the pthread_once( ) routine to ensure that your application initialization routine is executed only a single time; that is, by the first thread that tries to initialize the application. This routine is the only way to guarantee that one-time initialization is performed in a multithreaded environment on a given platform. The pthread_once( ) routine is of particular use for runtime libraries, which are often called for the first time after multiple threads are created.

Refer to the thr_intro(3thr) reference page for a list of the DCE Threads routines which, when called, implicitly perform any necessary initialization of the threads package. Any application that uses DCE Threads must call one of these routines before calling any other threads routines.