Article View: borland.public.cppbuilder.nativeapi
Article #2066Re: Need help with Mutex
From: Bob Gonder
Date: Sat, 28 Apr 2007 18:49
Date: Sat, 28 Apr 2007 18:49
52 lines
2474 bytes
2474 bytes
SubZero wrote:
>Ok. The original functions will be used with only TCriticalSection (single
>line). The reason for all the code is that it is needed for detecting the
>deadlock. I want to raise an exception so that MadExcept can show the stack!
>Hope you understand now. Sorry for the confusion.
>
>I cannot create the mutexes at startup because there are countless
>criticalsections. It must work within these two functions with named
>mutexes.
You still need to call CreateMutex with false, not true.
You still need to CloseHandle for every CreateMutex and OpenMutex.
Suggested outline:
whatever lockCritSec( whatever)
{
HANDLE M;
BOOL opened = true;
M = OpenMutex( flag, false, name);
if( M == NULL )
{ // create only if needed (first time)
opened = false;
M = CreateMutex( flag, false, name );
if( M == NULL )
{handle failure and return};
};
switch( WaitForSingleObject( M, 5000 ) )
{case WAIT_TIMEOUT:
/*whatever */
default:
break;
};
if( opened )
CloseHandle( M );
}
whatever releaseCritSec( whatever)
{
HANDLE M;
M = OpenMutex( flag, false, name);
if( M == NULL )
{handle failure and return};
// note that there is no wait() nor aquire at this point,
// we should already own the mutex
// and we only want to release it.
ReleaseMutex( M );
CloseHandle( M );
}
This way, at least you only have one dangling handle for each CritSec.
Technical Details
Message-ID:
<ff4733pjrik6vjq6d4cn5ec8or4ic42m9e@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4633777f$1@newsgroups.borland.com> <aev6339dmkoh5ih93feqeofs77v59f6iic@4ax.com> <4633887b@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,28184df4065864d1 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: Bob Gonder <notbg@notmindspring.invalid> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Need help with Mutex Date: Sat, 28 Apr 2007 11:49:38 -0700 Organization: MindSpring Enterprises, Inc. Reply-To: notbg@notmindspring.invalid Message-ID: <ff4733pjrik6vjq6d4cn5ec8or4ic42m9e@4ax.com> References: <4633777f$1@newsgroups.borland.com> <aev6339dmkoh5ih93feqeofs77v59f6iic@4ax.com> <4633887b@newsgroups.borland.com> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 67.77.147.157 X-Trace: newsgroups.borland.com 1177786176 67.77.147.157 (28 Apr 2007 11:49:36 -0700) Lines: 54 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail