Subj : Re: condvar wait / signal on arbitrary object To : comp.programming.threads From : Eric Sosman Date : Tue Jul 12 2005 04:09 pm Michael B Allen wrote: > I just had a thought and I'd like your opinion. > > Instead of embedding a condition variable in each object one would want > to wait and signal on, the object could be used as a key to lookup a > condvar by it's address modulo the size of an array of convars. This has > the benifit of permitting one to wait on an arbitrary object (like Java > except you can specify the mutex to unlock). For example: > > #define CONDVAR_ARRAY_SIZE 17 > > struct ctx { > pthread_cond_t array[CONDVAR_ARRAY_SIZE]; > }; > > int > ctx_pthread_cond_wait(struct ctx *ctx, void *obj, pthread_mutex_t *mutex) > { > pthread_cond_t *cond = ctx->array[(size_t)obj % CONDVAR_ARRAY_SIZE]; > return pthread_cond_wait(cond, mutex); > } > int > ctx_pthread_cond_signal(struct ctx *ctx, void *obj, pthread_mutex_t *mutex) > { > pthread_cond_t *cond = ctx->array[(size_t)obj % CONDVAR_ARRAY_SIZE]; > return pthread_cond_broadcast(cond); > } > > Of course depending on the table size there could be many spurious wakeups. > > What do you think? Good idea or should I not bother? What problem are you trying to solve? I've seen this technique used in a situation where there were very many very short-lived objects created and destroyed so rapidly that the overhead of pthread_cond_init()/destroy() was undesirable. (One wonders why these transient objects needed their own private condvars to begin with -- but strange artifacts often creep in when existing single-threaded code is "retrofitted" for multi-threading that wasn't envisioned in the original design.) The drawback, as you note, is that signals get replaced by broadcasts. One other thing: Since a condvar can only be associated with one mutex at a time, it'd be an error if two threads used two different mutexes to wait on two different objects that just happened to map to the same condvar. Perhaps an easy way to dodge this would be to put the mutex in the `struct ctx' and eliminate the `*mutex' argument from both functions. (In fact, it serves no purpose in ctx_pthread_cond_signal() as things now stand.) -- Eric.Sosman@sun.com .