Subj : Re: Is locking required for this scenario To : comp.programming.threads From : Joseph Seigh Date : Tue Feb 01 2005 12:54 pm On 1 Feb 2005 08:26:46 -0800, wrote: > I have a global container (of type List ) which I plan to initialize > during the daemon start up/initialization. During this time period > there will be no reads on this container. Once the daemon is > initialized multiple threads will simultaneously access the container > for reading it's contents (ie it will iterate through the container > contents). But there will be no modification of the container contents > during this phase. Does this container require synchronisation/locking. Only between the initialization of the container and the first use of the container by each thread. There are various ways you can implement that. One, use DCL (double checked locking) that works (advanced topic) or something like pthread_once that will initialize the container correctly. Or, you can initialized the container before starting any reader threads. The memory visibility rules for pthread_create guarantee that will work. Everything visible to pthread_create will be visible to the created threads. Or, have all the threads wait on a condition variable until the container is initialized, e.g. pthread_mutex_lock(&mutex); while (!container_ready) pthread_cond_wait(&cvar, &mutex); pthread_mutex_unlock(&mutex); Initialization code sets flag when finished, sets the flag, signals the condvar and releases the mutex. -- Joe Seigh .