Subj : Re: "Serialising" thread start-up... To : comp.programming.threads From : Giancarlo Niccolai Date : Wed Jan 05 2005 11:58 am Eric Sosman wrote: > Isn't there a race condition here? The new thread > could finish initializing and set `initialized = true;' > before pthread_create() returns, and then ... > > I think you should set `initialized = false;' before > calling pthread_create(). Alternatively, you could put > pthread_create() itself under protection of the mutex, > but it goes against the grain to hold a mutex any longer > than absolutely necessary. > You are right; the initialized variable must be initialized before thread fires up. Much better: >> while( thread_to_starts-- > 0 ) { initialized = false; create_th(myth ..., &initialized); >> lock( mutex ); >> while( ! initialized ) >> wait( cond, mutex ); >> ... unlock( mutex ); >> ... >> } As you are starting one thread at a time by hypothesis, "initialized" cannot be contended by different threads. Tnx for pointing out. About using pthread_create() in locks, somehow it often doesn't work. I didn't investigate it deeply, as it is anyhow to be avoided, but whenever I tried it I had problems (deadlocks or other unpleasing things). Maybe it was me that missed something, or maybe some implementation is just faulty under this circumstance (this includes some versions of WIN SDK). Bests, Giancarlo Niccolai. .