Re: [PATCH v3] Threaded grep
On Mon, Jan 18, 2010 at 12:11, Johannes Sixt <j.sixt@viscovery.net> wrote:
Show 17 quoted lines
> 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.
Show 8 quoted lines
>> +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