Article View: borland.public.cppbuilder.nativeapi
Article #2065Re: Need help with Mutex
From: SubZero
Date: Sat, 28 Apr 2007 17:46
Date: Sat, 28 Apr 2007 17:46
59 lines
3397 bytes
3397 bytes
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. Best Regards, SZ "Bob Gonder" <notbg@notmindspring.invalid>, iletisinde sunu yazdi, news:aev6339dmkoh5ih93feqeofs77v59f6iic@4ax.com... > SubZero wrote: > >>which does not work for no obvious reason. It simply raises the exception >>when there is no reason! > > What exception would that be? > > Why would you want to use _both_ mutex and CriticalSection ? > > I understand you want a timeout, so you use mutex, but that should be > enough. > > I think it would be better to have a global handle for the mutex so > you aren't constantly creating and opening the mutex (which you don't > use CloseHandle on, so are leaking resources). > > Your lockCriticalSection() calls CreateMutex() with true, which is not > correct for this. Think what happens if 2 threads (or applications) > try to lock at the same time. "Create with ownership" should fail for > one of them. > > Furthermore: > <help> > The thread that owns a mutex can specify the same mutex in repeated > wait function calls without blocking its execution. Typically, you > would not wait repeatedly for the same mutex, but this mechanism > prevents a thread from deadlocking itself while waiting for a mutex > that it already owns. However, to release its ownership, the thread > must call ReleaseMutex once for each time that the mutex satisfied a > wait. > </help> > What this means is your CreateMutex(true) satisfies one "ownership", > and the wait() satisfies a second, but you only release once. > > IMO you should CreateMutex(false) in application startup, with > CloseHandle() at shutdown. > Then the Lock() and and Release() would just use the global mutex > handle. > BTW, since your code should not reach releaseLock() without having > aquired a lock, releaseLock() should not wait(), but simply > ReleaseMutex(). > > >
Technical Details
Message-ID:
<4633887b@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<4633777f$1@newsgroups.borland.com> <aev6339dmkoh5ih93feqeofs77v59f6iic@4ax.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: "SubZero" <g_ates@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4633777f$1@newsgroups.borland.com> <aev6339dmkoh5ih93feqeofs77v59f6iic@4ax.com> In-Reply-To: <aev6339dmkoh5ih93feqeofs77v59f6iic@4ax.com> Subject: Re: Need help with Mutex Date: Sat, 28 Apr 2007 20:46:26 +0300 Lines: 60 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="ISO-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit 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: 85.99.193.41 Message-ID: <4633887b@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1177782395 85.99.193.41 (28 Apr 2007 10:46:35 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail