Article View: borland.public.cppbuilder.nativeapi
Article #2070Re: Need help with Mutex
From: SubZero
Date: Sun, 29 Apr 2007 08:20
Date: Sun, 29 Apr 2007 08:20
142 lines
4174 bytes
4174 bytes
Hello,
I thought I could get rid of the mutexes with the below code:
void inline __fastcall lockCriticalSection(TCriticalSection
*criticalSection)
{
for(int i = 0; i < 5000; ++i)
{
if(criticalSection->TryEnter())
return;
::Sleep(1);
}
throw Exception("BUG FOUND, CHECK CALL STACK!");
//criticalSection->Acquire();
};
but it never fires the exception yet it still deadlocks! :((
Regards,
SZ
"SubZero" <g_ates@hotmail.com>, iletisinde �unu yazd�,
news:4633777f$1@newsgroups.borland.com...
> Hello,
>
> I used to have an application with the following functions for thread
> sync'in.
>
> void inline __fastcall lockCriticalSection(TCriticalSection
> *criticalSection)
> {
> criticalSection->Acquire();
> };
>
> void inline __fastcall releaseCriticalSection(TCriticalSection
> *criticalSection)
> {
> criticalSection->Release();
> };
>
>
> To detect a deadlock, I implemented the below:
>
> void inline __fastcall lockCriticalSection(TCriticalSection
> *criticalSection)
> {
> HANDLE hMutex;
>
> // Create a mutex with no initial owner.
>
> hMutex = CreateMutex(
> NULL, // no security attributes
> true, // initially owned
> String((int)(void*)criticalSection).c_str()); // name of mutex
>
> DWORD dwWaitResult;
>
> // Request ownership of mutex.
>
> if(hMutex)
> {
> dwWaitResult = WaitForSingleObject(
> hMutex, // handle to mutex
> 5000L); // five-second time-out interval
>
> switch(dwWaitResult)
> {
> // Cannot get mutex ownership due to time-out.
> case WAIT_TIMEOUT:
> throw Exception("BUG FOUND, CHECK CALL STACK!");
>
> // Got ownership of the abandoned mutex object.
> case WAIT_ABANDONED:
> break;
> }
> }
>
> criticalSection->Acquire();
> };
>
> void inline __fastcall releaseCriticalSection(TCriticalSection
> *criticalSection)
> {
> criticalSection->Release();
>
> HANDLE hMutex;
>
> // Create a mutex with no initial owner.
>
> hMutex = OpenMutex(
> MUTEX_ALL_ACCESS,
> false,
> String((int)(void*)criticalSection).c_str()
> ); // name of mutex
>
> if(hMutex)
> {
> DWORD dwWaitResult;
>
> // Request ownership of mutex.
>
> dwWaitResult = WaitForSingleObject(
> hMutex, // handle to mutex
> 5000L); // five-second time-out interval
>
> switch(dwWaitResult)
> {
> // The thread got mutex ownership.
> case WAIT_OBJECT_0:
> // Release ownership of the mutex object.
> if(!ReleaseMutex(hMutex))
> {
> // Deal with error.
> }
>
> break;
>
> // Cannot get mutex ownership due to time-out.
> case WAIT_TIMEOUT:
> break;
>
> // Got ownership of the abandoned mutex object.
> case WAIT_ABANDONED:
> break;
> }
> }
> };
>
> which does not work for no obvious reason. It simply raises the exception
> when there is no reason! The Critical section variable is -1, 0 at that
> time!
>
> Please help!
>
> Best Regards,
>
> SZ
Technical Details
Message-ID:
<46345574@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4633777f$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,28184df4065864d1 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII From: "SubZero" <g_ates@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4633777f$1@newsgroups.borland.com> In-Reply-To: <4633777f$1@newsgroups.borland.com> Subject: Re: Need help with Mutex Date: Sun, 29 Apr 2007 11:20:56 +0300 Lines: 143 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-9"; reply-type=response Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Windows Mail 6.0.6000.16386 X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6000.16386 NNTP-Posting-Host: 88.230.146.11 Message-ID: <46345574@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1177834868 88.230.146.11 (29 Apr 2007 01:21:08 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail