Is locking required for this scenario

Is locking required for this scenario

Post by nin23 » Thu, 03 Feb 2005 01:26:46


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.
Ninan
 
 
 

Is locking required for this scenario

Post by Joseph Sei » Thu, 03 Feb 2005 02:54:58


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

 
 
 

Is locking required for this scenario

Post by Torsten Ro » Thu, 03 Feb 2005 06:12:05


if you are done with the initialization of the container before you
start any thread that will read that container later, there is a good
chance that you will not need any locking. But this will depend on the
implementation details of the container too. I've seen some silly static
variables in even commercial C++ standard container implementations.

regards
Torsten