Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion condvar wait / signal on arbitrary object

View parsed - Show only message text

Path: g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 13 Jul 2005 00:44:41 -0500
From: "Chris Thomasson" <_no_damn_spam_cristom@_no_damn_comcast.net_spam>
Newsgroups: comp.programming.threads
References: <pan.2005.07.12.18.46.24.794166@ioplex.com> <db14gm$fam$1@news1brm.Central.Sun.COM> <pan.2005.07.13.01.58.01.178104@ioplex.com>
Subject: Re: condvar wait / signal on arbitrary object
Date: Tue, 12 Jul 2005 22:47:59 -0700
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
Message-ID: <vY2dnRGhFKTUN0nfRVn-hQ@comcast.com>
Lines: 72
NNTP-Posting-Host: 67.161.1.7
X-Trace: sv3-8LpUv6NOKQAHbRwdK3G8TTQhx6kcaG8x9lIunUbyDgS+dk7+Pv+17V2uW5NL/oxd+vrpn5euY6h2WHM!iwhjOllBszmN7++NqEr9q4BBAVY2pLqL/aW3D2aCO2yjTfowQnln/uef9uHYscsSIhiiVxQ=
X-Complaints-To: abuse@comcast.net
X-DMCA-Complaints-To: d...@comcast.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.32

> 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!
...



?


;) 



Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google