Google Groups Home Help | Sign in
recursive mutexes
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 129 - Collapse all   Newer >
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
red floyd  
View profile
 More options Jul 15 2004, 3:09 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: red floyd <no.s...@here.dude>
Date: Thu, 15 Jul 2004 19:09:51 GMT
Local: Thurs, Jul 15 2004 3:09 pm
Subject: recursive mutexes

Does POSIX define what happens when a pthread currently holding a mutex
locks the same mutex?

i.e.

void f()
{
    pthread_mutex_lock(&some_mutex);
    pthread_mutex_lock(&some_mutex);

}

The Linux (MDK9.1) implementation of pthreads has a non-portable mutex
attribute letting you specify what behavior you want in said situation.

I'd like to try to write portable code, though.


    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.
j...@invalid.address  
View profile
 More options Jul 15 2004, 3:15 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: j...@invalid.address
Date: Thu, 15 Jul 2004 19:15:12 GMT
Local: Thurs, Jul 15 2004 3:15 pm
Subject: Re: recursive mutexes

red floyd <no.s...@here.dude> writes:
> Does POSIX define what happens when a pthread currently holding a
> mutex locks the same mutex?

Yes, see

http://www.opengroup.org/onlinepubs/009695399/functions/pthread_mutex...

Joe
--
We can't all be heroes because someone has to sit on the curb and
clap as they go by.
  - Will Rogers


    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.
Casper H. S. Dik  
View profile
 More options Jul 15 2004, 3:55 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: Casper H.S. Dik <Casper....@Sun.COM>
Date: 15 Jul 2004 19:55:24 GMT
Local: Thurs, Jul 15 2004 3:55 pm
Subject: Re: recursive mutexes

red floyd <no.s...@here.dude> writes:
>Does POSIX define what happens when a pthread currently holding a mutex
>locks the same mutex?
>i.e.
>void f()
>{
>    pthread_mutex_lock(&some_mutex);
>    pthread_mutex_lock(&some_mutex);
>}
>The Linux (MDK9.1) implementation of pthreads has a non-portable mutex
>attribute letting you specify what behavior you want in said situation.
>I'd like to try to write portable code, though.

Portable:

pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.


    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.
red floyd  
View profile
 More options Jul 15 2004, 4:54 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: red floyd <no.s...@here.dude>
Date: Thu, 15 Jul 2004 20:54:56 GMT
Local: Thurs, Jul 15 2004 4:54 pm
Subject: Re: recursive mutexes

OK, thanks Joe and Casper.  I guess my systems implementation is older,
and thus the recursive mutex hadn't yet been standardized or something.
So I'll simply #define something like this

#ifdef USE_NON_PORTABLE_RECURSIVE_MUTEX_
#define RECURSIVE_MUTEX_ATTR    PTHREAD_MUTEX_RECURSIVE_NP  /* MDK9.1 */
#else
#define RECURSIVE_MUTEX_ATTR    PTHREAD_MUTEX_RECURSIVE     /* other */
#endif

and use RECURSIVE_MUTEX_ATTR, so that I don't have to worry about it in
the future.


    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.
Alexander Terekhov  
View profile
 More options Jul 15 2004, 5:29 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: Alexander Terekhov <terek...@web.de>
Date: Thu, 15 Jul 2004 23:29:27 +0200
Local: Thurs, Jul 15 2004 5:29 pm
Subject: Re: recursive mutexes

red floyd wrote: ...

Don't use recursive mutexes. It's akin to sex with used condoms. ;-)

regards,
alexander.


    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.
Loic Domaigne  
View profile
 More options Jul 15 2004, 7:42 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: Loic Domaigne <loic-...@gmx.net>
Date: 15 Jul 2004 23:42:20 +0000
Local: Thurs, Jul 15 2004 7:42 pm
Subject: Re: recursive mutexes
Hello,

> Don't use recursive mutexes. It's akin to sex with used condoms. ;-)

what's going on, Alexander? Are you now using now sentences that everybody can
understand?

I am not used to that ;-)

Cheers,
Loic.
--
Article posté via l'accès Usenet http://www.mes-news.com
Accès par Nnrp ou Web


    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.
SenderX  
View profile
 More options Jul 15 2004, 7:45 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: "SenderX" <x...@xxx.com>
Date: Thu, 15 Jul 2004 23:45:07 GMT
Local: Thurs, Jul 15 2004 7:45 pm
Subject: Re: recursive mutexes

> Don't use recursive mutexes. It's akin to sex with used condoms. ;-)

YIKES!

    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.
red floyd  
View profile
 More options Jul 15 2004, 7:45 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: red floyd <no.s...@here.dude>
Date: Thu, 15 Jul 2004 23:45:08 GMT
Local: Thurs, Jul 15 2004 7:45 pm
Subject: Re: recursive mutexes

Alexander Terekhov wrote:
> red floyd wrote: ...

> Don't use recursive mutexes. It's akin to sex with used condoms. ;-)

> regards,
> alexander.

May I ask why?

    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.
David Schwartz  
View profile
 More options Jul 15 2004, 7:59 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: "David Schwartz" <dav...@webmaster.com>
Date: Thu, 15 Jul 2004 16:59:58 -0700
Local: Thurs, Jul 15 2004 7:59 pm
Subject: Re: recursive mutexes

red floyd wrote:
> May I ask why?

    There are two possibilities:

    1) You don't know the mutex is being used recursively. In this case, the
recursive mutex will hide a serious problem.

    2) You know the mutex is being recursively. In this case, just don't
lock it since you know it's already locked anyway.

    The problem is that recursive mutexes hide something from you and that
something is extremely important. Consider code like this:

A) Lock mutex
B) Unlock mutex
C) Do something, assuming the mutex is unlocked

    What happens if the mutex is recursive?

    The only value of a recursive mutex is that it allows you to write a
function that works properly whether or not a mutex is locked. But the
caller must know that the mutex is locked anyway, so why not just have two
versions of the function? (With the one you call without a lock possibly
just being a wrapper that grabs the lock and calls the other function.)

    It's just too hard and dangerous to write sensible code that works with
a mutex that might or might not start out with that mutex locked and with no
way to tell which. And the only value of recursive mutexes is that they let
you do this.

    DS


    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.
SenderX  
View profile
 More options Jul 15 2004, 8:21 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: "SenderX" <x...@xxx.com>
Date: Fri, 16 Jul 2004 00:21:05 GMT
Local: Thurs, Jul 15 2004 8:21 pm
Subject: Re: recursive mutexes

>     It's just too hard and dangerous to write sensible code that works
with
> a mutex that might or might not start out with that mutex locked and with
no
> way to tell which. And the only value of recursive mutexes is that they
let
> you do this.

There good for hashed locks.

    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.
David Schwartz  
View profile
 More options Jul 15 2004, 10:47 pm
Newsgroups: comp.programming.threads, comp.unix.programmer
From: "David Schwartz" <dav...@webmaster.com>
Date: Thu, 15 Jul 2004 19:47:24 -0700
Local: Thurs, Jul 15 2004 10:47 pm
Subject: Re: recursive mutexes

SenderX wrote:
>>     It's just too hard and dangerous to write sensible code that
>> works with a mutex that might or might not start out with that mutex
>> locked and with no way to tell which. And the only value of
>> recursive mutexes is that they let you do this.
> There good for hashed locks.

    I can't imagine how. The only way they might help is if you have code
that grabs more than one object and they might 'happen to' have the same
lock protecting them. But if your code does this, they might also happen to
have different locks protecting them. In this case, you must enforce your
lock ordering rules or else you can deadlock.

    In other words, if one thread can grab object A then object B, grabbing
lock 1 followed by lock 2, another thread could grab object C followed by
object D, grabbing lock 2 followed by lock 1. Then you deadlock.

    So if you're going to use hashed locks, you may either hold two locks at
once or you may not. If you may not, recursive locks don't help.

    If you may, then you must check to see what locks they refer to so that
you acquire them in the right order. While you're doing this, it's just as
easy to check if they're the same lock, and if so only acquire it once. So
recursive locks don't help you in this case.

    Worse, recursive locks might lull you into thinking you don't have to
check what locks you are acquiring, and then you are vulnerable to lock
order reversal and deadlocks.

    DS


    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.
Giancarlo Niccolai  
View profile
 More options Jul 16 2004, 5:32 am
Newsgroups: comp.programming.threads, comp.unix.programmer
Followup-To: comp.programming.threads
From: Giancarlo Niccolai <no...@nothing.it>
Date: Fri, 16 Jul 2004 11:32:32 +0200
Local: Fri, Jul 16 2004 5:32 am
Subject: Re: recursive mutexes

red floyd wrote:
> Alexander Terekhov wrote:
>> red floyd wrote: ...

>> Don't use recursive mutexes. It's akin to sex with used condoms. ;-)

>> regards,
>> alexander.

> May I ask why?

Other than Mr. Schwartz reply (which I consider absolutely good), I may add
also that recursive mutexes are slower (they must check if they are locked
or not) and brings you to program less carefully about shared resource
usage. If you stop to think twice about that, you'll realize that when you
want to lock a resource, you just want to know that you are the owner of
that, and not that you've been entitled to be the owner N times...

The only reason I can see for recursive mutexes is to call recursive
functions, where the locks are so complex that writing a wrapper is not a
viable way, or to create "onion peel" like libraries where the lower levels
are completely obscure and abstracted to the higher ones, and there is
sometimes the need to "extend" the mutex locking also to some function in
the higher levels; but in both the cases there are probably better ways to
deal with the problem with a non recursive mutex, with a little more
cleaner design effort.

Giancarlo.


    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.
Martin James  
View profile
 More options Jul 16 2004, 10:59 am
Newsgroups: comp.programming.threads, comp.unix.programmer
From: "Martin James" <mjames_fal