Article View: borland.public.cppbuilder.nativeapi
Article #2063Re: Need help with Mutex
From: Bob Gonder
Date: Sat, 28 Apr 2007 17:31
Date: Sat, 28 Apr 2007 17:31
41 lines
2563 bytes
2563 bytes
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:
<aev6339dmkoh5ih93feqeofs77v59f6iic@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!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-7-bit From: Bob Gonder <notbg@notmindspring.invalid> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Need help with Mutex Date: Sat, 28 Apr 2007 10:31:25 -0700 Organization: MindSpring Enterprises, Inc. Reply-To: notbg@notmindspring.invalid Message-ID: <aev6339dmkoh5ih93feqeofs77v59f6iic@4ax.com> References: <4633777f$1@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 1177781482 67.77.147.157 (28 Apr 2007 10:31:22 -0700) Lines: 44 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail