🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: ensuring CriticalSections are release..

#796
From: AlexB
Date: Mon, 14 Jan 2008 09:37
45 lines
2305 bytes
Mike Collins 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?
> 
> I have a thread with a getter and setter to provide access to a
> private variable.  I use a CRITICAL_SECTION to ensure thead-safety
> but i wasn't sure about whether or not to use a try.. __finally to
> ensure that the CS always gets released...

Yes, you can write:

int MyClassGetProp(void)
{
 int res;
 EnterCtiticalSection(&CS);
 try
 {
  /*some code with possible exception*/
  ...
  res=/*some result*/;
 }
 __finally
 {
  LeaveCriticalSection(&CS); // will be executed in any case
 }
 return res;
}

Due to some problems in compiler you should
1) avoid direct ("goto") or indirect ("return") leaving try section.
'Return' is not prohibited but generates a lot of binary code
(intermediate variable for result is much better);
2) don't throw exceptions in __finally section (in theory it should
generate abort but it doesn't);
3) avoid complicated exception handling in __finally (see QC30802)
4) NEVER use 'goto' or 'return' for leaving __finally section.
5) use CB2007 where some problems were fixed (like QC21708)

-- 
Alex

P.S. Please, don't beat me for the post: I know that try/__finally
isn't pure cpp way :-)
Technical Details
Message-ID: <478b3b60@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder3.cambrium.nl!feeder5.cambrium.nl!feed.tweaknews.nl!68.142.88.75.MISMATCH!hwmnpeer01.ams!news.highwinds-media.com!news.astraweb.com!newsrouter-eu.astraweb.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!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: "AlexB" <b.a.v@inbox.ru>
Subject: Re: ensuring CriticalSections are release..
Newsgroups: borland.public.cppbuilder.nativeapi
References:  <478a96e3@newsgroups.borland.com>
User-Agent: XanaNews/1.18.1.6
NNTP-Posting-Host: 212.192.185.54
Message-ID: <478b3b60@newsgroups.borland.com>
Date: 14 Jan 2008 02:37:20 -0700
X-Trace: newsgroups.borland.com 1200307040 212.192.185.54 (14 Jan 2008 02:37:20 -0700)
Lines: 45
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder3.cambrium.nl!feeder5.cambrium.nl!feed.tweaknews.nl!68.142.88.75.MISMATCH!hwmnpeer01.ams!news.highwinds-media.com!news.astraweb.com!newsrouter-eu.astraweb.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail