Subj : Re: condvar wait / signal on arbitrary object To : comp.programming.threads From : Chris Thomasson Date : Tue Jul 12 2005 11:47 pm > Or (I'm not certain that this actually makes sense but ...) each condvar > could have its own mutex. > > struct ctx { > pthread_cond_t condvars[CTX_ARRAY_SIZE]; > pthread_mutex_t mutexes[CTX_ARRAY_SIZE]; > }; > ... > int > ctx_pthread_cond_wait(struct ctx *ctx, void *obj) > { > int idx = (size_t)obj % CTX_ARRAY_SIZE; > pthread_cond_t *cond = ctx->condvars[idx]; > pthread_mutex_t *mutex = ctx->mutexes[idx]; > return pthread_cond_wait(cond, mutex); > } You can get into potential deadlock situations here, if your not careful... static ctx locks; static obj a, b, c; Thread A ---------- 1: ctx_pthread_mutex_lock( &locks, &a ); 2: ctx_pthread_mutex_lock( &locks, &b ); 3: ctx_pthread_mutex_unlock( &locks, &b ); 4: ctx_pthread_mutex_unlock( &locks, &a ); Thread B ---------- 1: ctx_pthread_mutex_lock( &locks, &b ); 2: ctx_pthread_mutex_lock( &locks, &c ); 3: ctx_pthread_mutex_unlock( &locks, &c ); 4: ctx_pthread_mutex_unlock( &locks, &b ); Lets say that the objects map to the following mutexs: &a = mutex1 &b = mutex2 &c = mutex1 and the execution sequence happens to go like this: A1 B1 A2 -- deadlocked! .... ? ;) .