Subj : Re: Did pthread_once init_routine succeed? To : comp.programming.threads From : David Schwartz Date : Wed Feb 23 2005 04:25 pm "Ian Pilcher" wrote in message news:ksidnY-GyNgDkoDfRVn-2w@comcast.com... > Is it safe to use a file-scope variable to report the result of an > init_routine called via pthread_once? I believe that it is, if the > following conditions are met: Yes. What would be the use of pthread_once if you couldn't modify memory in the initialization routine and access the results after pthread_once returns? (Think about it, that's about all pthread_once is good for.) > * The variable is only set by the init_routine. > > * The variable is only read immediately following a call to > pthread_once (with the appropriate parameters). > > It seems to me that one of three things can happen when a thread hits a > call to pthread_once: > > 1. The thread is the "first" to call pthread_once (with these > parameters). It will execute the init_routine, which sets the > value of the variable, and then return from pthread_once. The > value of the variable is determinate when the thread reads it. > > 2. The thread is not the "first" to call pthread_once, but the > init_routine has not yet returned (in whatever thread it is > executing). The thread will block until the other thread > completes the init_routine (setting the variable). The value > of the variable is determinate when the thread reads it. > > 3. The thread is not the "first" to call pthread_once, and > init_routine has already completed (in this or another thread). > The variable was set by init_routine, so its value is determinate > when the thread reads it. Correct, this is the semantics of pthread_once and the reason it is useful at all. DS .