🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #794

Re: ensuring CriticalSections are release..

#794
From: Alan Bellingham
Date: Mon, 14 Jan 2008 00:10
26 lines
2016 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote:

>Hay all, just a quicky, just wondering if anyone had any thoughts on using 
>try... __finally to ensure that a critical section is always released?

Don't do it.

Make an object that has a destructor. In the destructor, put the code to
release the critical section. Now just put the object on the stack -
when the current scope is exited, the critical section *WILL* be
released.

This is a fundamental idea in C++, known as 'Resource Allocation Is
Initialisation' or RAII. You make sure that any resource that must be
allocated and released is owned by an object that, if it still has it
when it gets to destruct, releases it. std::string works this way.
std::fstream works this way. std::vector works this way.

Using try/__finally is not the C++ way, it's the way of Java and other
languages that can't use deterministic destruction.

Alan Bellingham
-- 
Team Browns
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html>      netiquette
Technical Details
Message-ID: <0s6lo3pjv2ggjubavc8l673et5h0ebcer2@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns13feed!worldnet.att.net!64.192.187.27!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <478a96e3@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,266d339b0fd4ba38
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: Alan Bellingham <alanb@episys.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: ensuring CriticalSections are release..
Date: Mon, 14 Jan 2008 00:10:12 +0000
Reply-To: alanb@episys.com
Message-ID: <0s6lo3pjv2ggjubavc8l673et5h0ebcer2@4ax.com>
References: <478a96e3@newsgroups.borland.com>
X-Newsreader: Forte Agent 4.2/32.1118
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 62.49.31.105
X-Trace: newsgroups.borland.com 1200269410 62.49.31.105 (13 Jan 2008 16:10:10 -0700)
Lines: 26
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns13feed!worldnet.att.net!64.192.187.27!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail