Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages
Started by static array?
Tue, 17 Jul 2007 15:17
My Mutex stupidity - Help please
Author: static array?
Date: Tue, 17 Jul 2007 15:17
Date: Tue, 17 Jul 2007 15:17
23 lines
1608 bytes
1608 bytes
I was having issues with my mutex handler - and have boiled it down to this. What the heck is wrong with this. I expect to create a deadlock here - but it doesnt happen! char * c = "ksfgkjsfdgkj\0"; HANDLE MutexHandle1 = CreateMutex(NULL, FALSE, c); WaitForSingleObject(MutexHandle1, INFINITE); // ok so I have the mutex lock now // lets try to create (open) the same mutex. (I note that the handle // address returned is different for each call). HANDLE MutexHandle2 = CreateMutex(NULL, FALSE, c); // note exact same name WaitForSingleObject(MutexHandle2, INFINITE); // I expect that last call to never return - but it does immediately. // I also note that a call to GetLastError does indeed return ALREADY_EXISTS.... so I am confused... What am I doing wrong here?
Re: My Mutex stupidity - Help please
Author: Bruce Salzman
Date: Tue, 17 Jul 2007 15:25
Date: Tue, 17 Jul 2007 15:25
45 lines
2281 bytes
2281 bytes
"static array?" <nospam@nospam.com> wrote in message news:469cdda8@newsgroups.borland.com... >I was having issues with my mutex handler - and have boiled it down >to this. > > What the heck is wrong with this. I expect to create a deadlock > here - but it doesnt happen! > > char * c = "ksfgkjsfdgkj\0"; > > HANDLE MutexHandle1 = CreateMutex(NULL, FALSE, c); > WaitForSingleObject(MutexHandle1, INFINITE); > > // ok so I have the mutex lock now > > // lets try to create (open) the same mutex. (I note that the > handle > // address returned is different for each call). > > HANDLE MutexHandle2 = CreateMutex(NULL, FALSE, c); // note exact > same name > WaitForSingleObject(MutexHandle2, INFINITE); > > // I expect that last call to never return - but it does > immediately. > // I also note that a call to GetLastError does indeed return > ALREADY_EXISTS.... so I am confused... > > What am I doing wrong here? From the Windows SDK: "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." -- Bruce
Re: My Mutex stupidity - Help please
Author: Bob Gonder
Date: Tue, 17 Jul 2007 15:36
Date: Tue, 17 Jul 2007 15:36
21 lines
1803 bytes
1803 bytes
static array? wrote: >What the heck is wrong with this. I expect to create a deadlock here - but >it doesnt happen! Once you own the mutex, you own it until you release it. Waiting on a mutex you own always returns, otherwise you would deadlock. >HANDLE MutexHandle1 = CreateMutex(NULL, FALSE, c); >WaitForSingleObject(MutexHandle1, INFINITE); >HANDLE MutexHandle2 = CreateMutex(NULL, FALSE, c); // note exact same name <Help> Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. </Help> So, it is a different handle to the same mutex, which you own. Still no deadlock.
Re: My Mutex stupidity - Help please
Author: Remy Lebeau (TeamB)
Date: Tue, 17 Jul 2007 17:13
Date: Tue, 17 Jul 2007 17:13
36 lines
2288 bytes
2288 bytes
"static array?" <nospam@nospam.com> wrote in message
news:469cdda8@newsgroups.borland.com...
> What the heck is wrong with this. I expect to create a
> deadlock here - but it doesnt happen!
Why would you expect that? A thread is allowed to obtain a lock for a mutex
it already has a lock to, specifically so that it WON'T deadlock itself.
This is clearly stated in the documentation for CreateMutex():
"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."
> I expect that last call to never return - but it does immediately.
It is supposed to. The HANDLE values may be different, but they point to
the same physical mutex object. The thread already owns the mutex from the
first call to WaitForSingleObject(), so the second call exits immediately.
> I also note that a call to GetLastError does indeed return
> ALREADY_EXISTS....
It is supposed to. You passed the same name to both calls to CreateMutex().
> What am I doing wrong here?
Nothing at all. You simply are not reading the documentation. This
behavior is working as-designed.
Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads