Mutexes and WaitForSingleObject -> doesn´t wait ??
-
Hello, I just played around with mutexes and wrote the following piece of code:
HANDLE hMutex = CreateMutex(NULL, // no security descriptor needed in the beginning
TRUE, // not initial owner
"test"); // mutex name (hopefully unique)DWORD err = WaitForSingleObject(hMutex, INFINITE);
if( err == WAIT_FAILED )
printf( "This shouldn´t happen." );
else
{
err = WaitForSingleObject(hMutex, INFINITE);
if( err == WAIT_FAILED )
printf( "This shouldn´t happen." );
else
printf( "This shouldn´t happen either." );
}No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use? I guess, I have some general trouble understanding the mechanism here. How do I get the mutex to lock a code area up? Souldrift
-
Hello, I just played around with mutexes and wrote the following piece of code:
HANDLE hMutex = CreateMutex(NULL, // no security descriptor needed in the beginning
TRUE, // not initial owner
"test"); // mutex name (hopefully unique)DWORD err = WaitForSingleObject(hMutex, INFINITE);
if( err == WAIT_FAILED )
printf( "This shouldn´t happen." );
else
{
err = WaitForSingleObject(hMutex, INFINITE);
if( err == WAIT_FAILED )
printf( "This shouldn´t happen." );
else
printf( "This shouldn´t happen either." );
}No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use? I guess, I have some general trouble understanding the mechanism here. How do I get the mutex to lock a code area up? Souldrift
Whay don't you use GetLastError() when the return value is WAIT_FAILED as the documentation suggests?
-
Whay don't you use GetLastError() when the return value is WAIT_FAILED as the documentation suggests?
-
Hello, I just played around with mutexes and wrote the following piece of code:
HANDLE hMutex = CreateMutex(NULL, // no security descriptor needed in the beginning
TRUE, // not initial owner
"test"); // mutex name (hopefully unique)DWORD err = WaitForSingleObject(hMutex, INFINITE);
if( err == WAIT_FAILED )
printf( "This shouldn´t happen." );
else
{
err = WaitForSingleObject(hMutex, INFINITE);
if( err == WAIT_FAILED )
printf( "This shouldn´t happen." );
else
printf( "This shouldn´t happen either." );
}No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use? I guess, I have some general trouble understanding the mechanism here. How do I get the mutex to lock a code area up? Souldrift
Souldrift wrote:
No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use?
Souldrift wrote:
I guess, I have some general trouble understanding the mechanism here.
You're missing this ([^])
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.
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Souldrift wrote:
No I don´t understand why the second WaitForSingleObject()-call doesn´t block. Since I said to wait indefinitely, shouldn´t it do so because the mutex object is already in use?
Souldrift wrote:
I guess, I have some general trouble understanding the mechanism here.
You're missing this ([^])
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.
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]