Reputation: 3744
Consider this code:
#include <Windows.h>
test(HANDLE*hB0)
{
printf("hello "), ReleaseMutex(*hB0);
}
main()
{
HANDLE hB0;
CreateThread(0,0,test,(hB0=CreateMutex(NULL,true,NULL),&hB0),0,0),
WaitForSingleObject(hB0, INFINITE),
printf("world!");
}
I'm expecting to get hello world! but I'm just getting world! instead. Any ideas why?
I tried with second parameter to CreateMutex both true & false but it is still not working.
Upvotes: 0
Views: 325
Reputation: 33706
from CreateMutex
function
The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution
in call
hB0=CreateMutex(NULL,true,NULL)
the bInitialOwner = TRUE
so your current thread own the mutex, as result WaitForSingleObject(hB0, INFINITE)
just return without blocking execution.
from another side if thread, which not own mutex, call ReleaseMutex
- will be error returned ERROR_NOT_OWNER
- Attempt to release mutex not owned by caller. or native status STATUS_MUTANT_NOT_OWNED
An attempt to release a mutant object was made by a thread that was not the owner of the mutant object. - but you not check function result and error code.
also because you define test(HANDLE*hB0)
- you need call ReleaseMutex(*hB0);
instead ReleaseMutex(hB0);
. however better pass hB0
handle direct to thread as argument, instead pointer &hB0
to handle.
Upvotes: 3