🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.programming.threads
Article #10

Re: Looking for best design for using pthreads in C++ objects

#10
From: Dave Butenhof
Date: Thu, 07 Mar 1996 12:48
47 lines
2937 bytes
David Brownell wrote:
> 
> Webb Scales wrote:
> >
> > David Brownell wrote:
> > > One of the really nice techniques is to have a "Locker"
> > > class to grab mutexes
> > > as needed, and then release it automatically on all exits.  [...]
> > > That kind of class really helps get rid of the bugs you have due
> > > to locks not getting released uniformly on all codepaths.
> >
> > Hmmm...this approach makes me nervous.  I assert that it's better
> > to neglect to unlock a mutex than to unlock it when you shouldn't have.
> 
> Depends on how you're using the mutex; both bugs are deadly.  Did the
> victim die because of the loss of blood first, or the kidney failure?
> (Don't you just hate it when police beat up suspects?)
> 
 [...]
> 
> More complex invariants can be encapsulated in other classes rather
> easily, and should be.  As in fact Webb noted later.  I didn't have time
> to write out a complete C++/MT style guide; that point is a logical
> combination of (a) encapsulation, and (b) the "Locker" technique.

But that's exactly what Webb was trying to say. The solution is, as you
say, a COMBINATION of encapsulation and "the Locker technique". Locker
alone will not solve most problems, and may create more. By claiming
that "it" is a solution, you may mislead some folks to believe that
putting Locker into a routine will solve all possible problems. That's
incorrect and dangerous.

Having classes with destructors that clean up state, as TRIVIALLY
DEMONSTRATED by Locker, is clearly the best way to apply C++ semantics to
the problem of protecting thread state invariants against "unusual"
routine exit. But the true solution in nearly all cases must go beyond
the triviality of Locker to restore data invariants, not merely the state
of the mutex!

> - Dave

------------------------------------------------------------------------
Dave Butenhof                              Digital Equipment Corporation
butenhof@zko.dec.com                       110 Spit Brook Rd, ZKO2-3/Q18
Phone: 603.881.2218, FAX: 603.881.0120     Nashua, NH 03062-2711
                 "Better Living Through Concurrency"
------------------------------------------------------------------------
Technical Details
Message-ID: <313EDB0B.FF6@zko.dec.com>
Path: arc1-ca.pugleaf.net!zk2nws.zko.dec.com!usenet
References: <3128ff8b.666031216@news.clark.net> <312A0E5F.7B2C@ix.netcom.com> <31320705.41C6@zko.dec.com> <313DEF19.5B69@ix.netcom.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!zk2nws.zko.dec.com!usenet
From: Dave Butenhof <butenhof@zko.dec.com>
Newsgroups: comp.programming.threads,comp.lang.c++,comp.unix.osf.osf1,comp.unix.programmer,comp.object
Subject: Re: Looking for best design for using pthreads in C++ objects
Date: Thu, 07 Mar 1996 07:48:11 -0500
Organization: Digital Equipment Corportation
Message-ID: <313EDB0B.FF6@zko.dec.com>
References: <3128ff8b.666031216@news.clark.net> <312A0E5F.7B2C@ix.netcom.com> <31320705.41C6@zko.dec.com> <313DEF19.5B69@ix.netcom.com>
NNTP-Posting-Host: kalkin.zko.dec.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 2.0 (X11; I; OSF1 V4.0 alpha)