Article View: borland.public.cppbuilder.nativeapi
Article #15276Re: Mutex and WaitForSingleObject Problem...
From: Remy Lebeau (TeamB)
Date: Tue, 15 Jul 2003 02:27
Date: Tue, 15 Jul 2003 02:27
31 lines
2124 bytes
2124 bytes
"Colin Maharaj" <cbm@cablenett.net> wrote in message
news:3F135784.B6BE9C00@cablenett.net...
> if (BinAccess == NULL)
> {
> if (GetLastError() == ERROR_ALREADY_EXISTS)
If the mutex already existed, CreateMutex() will not return NULL. It only
does that on a real error. Your logic could be more like this instead:
HANDLE BinAccess = CreateMutex(NULL, FALSE, "BinAccess");
bool DidCreate = false
if( (BinAccess) && (GetLastError() != ERROR_ALREADY_EXISTS) )
DisCreate = true;
However...
> if (DidCreate == true) CloseHandle(BinAccess);
You must always close any handles you obtain, whether you initially created
them or not. So your logic to use DidCreate in the first place is wrong.
Get rid of it.
With that said, you should also not be creating/opening the mutex everything
time you want to wait on it, either. You should be creating it *once* at
the very beginning and then reusing it over and over for the lifetime of the
thread or data (depending on your design), only closing it onoce you're
actually finished with it.
Gambit
Technical Details
Message-ID:
<3f136552$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3F135784.B6BE9C00@cablenett.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,4b66d79a94ed9589 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-07-14 19:38:02 PST From: "Remy Lebeau \(TeamB\)" <gambit47@yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3F135784.B6BE9C00@cablenett.net> Subject: Re: Mutex and WaitForSingleObject Problem... Date: Mon, 14 Jul 2003 19:27:19 -0700 Lines: 33 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 207.175.226.26 Message-ID: <3f136552$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1058235730 207.175.226.26 (14 Jul 2003 19:22:10 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail