Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion condvar wait / signal on arbitrary object
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chris Thomasson  
View profile  
 More options Jul 13 2005, 1:47 am
Newsgroups: comp.programming.threads
From: "Chris Thomasson" <_no_damn_spam_cristom@_no_damn_comcast.net_spam>
Date: Tue, 12 Jul 2005 22:47:59 -0700
Local: Wed, Jul 13 2005 1:47 am
Subject: Re: condvar wait / signal on arbitrary object

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

?

;)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

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