From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,870f39ca0a91dafa X-Google-Attributes: gidfc89c,public X-Google-Thread: 1108a1,870f39ca0a91dafa X-Google-Attributes: gid1108a1,public X-Google-Thread: 109fba,870f39ca0a91dafa X-Google-Attributes: gid109fba,public X-Google-Thread: fee84,870f39ca0a91dafa X-Google-Attributes: gidfee84,public X-Google-Thread: ffc1e,870f39ca0a91dafa X-Google-Attributes: gidffc1e,public From: hf@colibri.de (Harald Fuchs) Subject: Re: Looking for best design for using pthreads in C++ objects Date: 1996/02/27 Message-ID: #1/1 X-Deja-AN: 141415066 x-pgp-fingerprint: 09 4E C4 A2 B2 C5 33 1A 79 80 5D 39 BD 9B 89 39 sender: hf@themroc.colibri.de x-disclaimer: Kore wa Colibri no iken dewa arimasen references: <3128ff8b.666031216@news.clark.net> <312A0E5F.7B2C@ix.netcom.com> x-www-home-page: http://www.colibri.de/~hf/ content-type: text/plain; charset=iso-8859-1 x-pgp-public-key: finger hf@colibri.de organization: Colibri GmbH, Germany mime-version: 1.0 newsgroups: comp.programming.threads,comp.lang.c++,comp.unix.osf.osf1,comp.unix.programmer,comp.object Webb Scales writes: > 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. Since the question was about pthreads in C++, you should use the technique presented nevertheless. The problem is that you can leave the critical section not only in the "normal" way (where unlock() gets executed), but also by means of an exception thrown deep in your calling chain. It's simply too easy to forget an appropriate catch clause.