From: Fredrik Kuivinen Date: Mon, 18 Jan 2010 13:28:26 GMT Subject: Re: [PATCH v3] Threaded grep Message-ID: <4c8ef71001180528h6182ef4l8a617fd038bd7318@mail.gmail.com> In-Reply-To: <4B5441D7.40503@viscovery.net> On Mon, Jan 18, 2010 at 12:11, Johannes Sixt wrote: > Fredrik Kuivinen schrieb: >> +/* This lock protects all the variables above. */ >> +static pthread_mutex_t grep_lock = PTHREAD_MUTEX_INITIALIZER; >> + >> +/* Signalled when a new work_item is added to todo. */ >> +static pthread_cond_t cond_add = PTHREAD_COND_INITIALIZER; >> + >> +/* Signalled when the result from one work_item is written to >> +   stdout. */ >> +static pthread_cond_t cond_write = PTHREAD_COND_INITIALIZER; >> + >> +/* Signalled when we are finished with everything. */ >> +static pthread_cond_t cond_result = PTHREAD_COND_INITIALIZER; > > Please do not use PTHREAD_MUTEX_INITIALIZER nor PTHREAD_COND_INITIALIZER; > call pthread_mutex_init and pthread_cond_init (and the corresponding > *_destroy!!) from the code. Ok. Will fix. >> +static void add_work(enum work_type type, char *name, char *buf, size_t size) >> +{ >> +... >> +     pthread_mutex_unlock(&grep_lock); >> +     pthread_cond_signal(&cond_add); > > Please swap these two lines, so that pthread_cond_signal() is called while > the lock is held. May I ask why? (just curious) Thanks for your comments. - Fredrik