Article View: borland.public.cppbuilder.nativeapi
Article #801Re: ensuring CriticalSections are release..
From: Remy Lebeau (TeamB)
Date: Mon, 14 Jan 2008 18:12
Date: Mon, 14 Jan 2008 18:12
89 lines
3147 bytes
3147 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message
news:478a96e3@newsgroups.borland.com...
> 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?
That wll work fine, as long as you do not have any 'return' statements
inside the 'try' section, as 'return' is an immediate exit that does not
allow '__finally' to run correctly.
A better approach is to use an RAII object instead, where the constructor
enters the critical section and the destructor leaves it. That way, the CS
is always guaranteed to be unlocked when the object goes out of scope, and
you don't need to use a try...__finally block at all.
> 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...
Either approach will work fine in that situation, ie:
--- try..finally ---
int MyThread::GetSomething()
{
int tmp;
EnterCriticalSection(&fMyCS);
try
{
tmp = fSomething;
}
__finally
{
LeaveCriticalSection(&fMyCS);
}
return tmp;
}
void MyThread::SetSomething(int Value)
{
EnterCriticalSection(&fMyCS);
try
{
// assign Something...
}
__finally
{
LeaveCriticalSection(&fMyCS);
}
}
--- RAII ---
class TCSGuard
{
private:
LPCRITICAL_SECTION fCS;
public:
TCSGuard(LPCRITICAL_SECTION aCS)
: fCS(aCS)
{
if( fCS ) EnterCriticalSection(fCS);
}
~TCSGuard()
{
if( fCS ) LeaveCriticalSection(fCS);
}
};
int MyThread::GetSomething()
{
TCSGuard g(&fMyCS);
return Something;
}
void MyThread::SetSomething(int Value)
{
TCSGuard g(&fMyCS);
// assign Something...
}
Gambit
Technical Details
Message-ID:
<478ba68e@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newshub.sdsu.edu!news.linkpendium.com!news.linkpendium.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<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: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <478a96e3@newsgroups.borland.com> Subject: Re: ensuring CriticalSections are release.. Date: Mon, 14 Jan 2008 10:12:45 -0800 Lines: 91 Organization: Lebeau Software X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 NNTP-Posting-Host: 207.178.157.90 Message-ID: <478ba68e@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1200334478 207.178.157.90 (14 Jan 2008 10:14:38 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newshub.sdsu.edu!news.linkpendium.com!news.linkpendium.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail