From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,dee25f6fd173ccc0 X-Google-Attributes: gidfee84,public From: Dave Butenhof Subject: Re: recursive mutex locks in posix threads Date: 1996/04/29 Message-ID: <318499DB.41C6@zko.dec.com>#1/1 X-Deja-AN: 152910090 references: <317FB0E9.76AB@iona.com> content-type: text/plain; charset=us-ascii organization: Digital Equipment Corporation mime-version: 1.0 newsgroups: comp.unix.osf.osf1 x-mailer: Mozilla 2.01 (X11; I; OSF1 V4.0 alpha) Terence Cross wrote: > > Hi, > In the OSF1 (Digital Unix) posix threads package > there is a recursive mutex. > This is not supposed to be used > as the lock for a condition variable > because if recursion is > 1 > then the coniditional wait > will fail to unlock the mutex. > So fast mutexes are suggested. > > However, for recursive mutexes, > why did the conditional not simply unlock > the recursive mutex many times > until it was free. Store that number > on the stack. On return pop the number > and lock it that many times. > > rgs, > /Terence Cross. Because if we did so, you might as well not have locked the mutex at all. Or, to put it another way, if you aren't protecting any data with the mutex, you should unlock it yourself. If you ARE protecting data with the mutex, it would be INCORRECT and DANGEROUS for the thread library to arbitrarily unlock the mutex "all the way". By the way, it is almost always a bad idea to make any call with a mutex locked. In about 99% of the cases where it really is necessary (for example, when calling a library that's not thread-safe), it is fruitless to use your own mutex to protect it. Some other code may use a different mutex to protect the same unsafe code -- and if it gets called concurrently in two different threads, each using a different mutex, then both callers might as well not have bothered. That's why DECthreads invented the "global lock" mutex... it is simply a "well known" public recursive mutex so that it's possible for all code in a process to agree on a single synchronization mechanism to protect unsafe code. This was very important when we first released DECthreads for ULTRIX, OpenVMS 5.5, and the various OSF DCE platforms, none of which had any support for threads beyond that provided by the thread library. Now, the global lock (and recursive mutexes in general) have about outlived their usefulness, and most operating systems have become more generally "thread aware" and the standard libraries are thread-safe out of the box. +-------------------------------------------------------------------+ | Dave Butenhof Digital Equipment Corporation | | butenhof@zko.dec.com 110 Spit Brook Rd, ZKO2-3/Q18 | | PH 603.881.2218, FAX 603.881.0120 Nashua, NH 03062-2711 | +----------+ "Better Living Through Concurrency" +----------+ +---------------------------------------------+