Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Seniors,
Can you please type-in what effect the code has to the following.
I want to use this in multi-threading program through C-Win32.

HANDLE hMutex;


Scenario: 1
-------------
VB
hMutex  = CreateMutex( NULL, FALSE, NULL );


Scenario: 2
-------------
VB
hMutex  = CreateMutex( NULL, TRUE, NULL );


if the initial owner is set to FALSE then what is the use of WaitForSingleObject( hMutex, INFINITE );
else if the initial owner is set to TRUE then what is the use of WaitForSingleObject( hMutex, INFINITE );

I know you would ask me to refer into msdn but i still want a more generic answer rather than a pure technical answer to this as i am a beginner to threads. I guess suggesting in a more generic way would make my basics much stronger.

Regards,
Posted
Updated 10-Oct-11 9:03am
v3

WaitForSingleObject blocks the current thread until the mutex is released, how you created the mutex does not affect it.
In your scenario 2 the mutex is initially owned by the current thread, and in your scenario 1 it is not.
 
Share this answer
 
Comments
[no name] 10-Oct-11 15:24pm    
I am really thankful to your suggestion. Can you also please elaborate on point 1.
Simon Bang Terkildsen 10-Oct-11 15:50pm    
ok say you have a mutex which is owned by thread01 then when you call WaitForSingleObject thread02 it will not return until the mutex has been released by thread01. Once it returns the mutex is owned by thread02 if and only if the return value is WAIT_OBJECT_0 or WAIT_ABANDONED, it would return another value if it timeout or the mutex handle were closed.
Sergey Alexandrovich Kryukov 10-Oct-11 16:51pm    
Right, a 5.
--SA
Espen Harlinn 13-Oct-11 17:26pm    
Excellent, my 5
Simon Bang Terkildsen 14-Oct-11 5:57am    
Thank you, Espen.
Way back in ancient history when I was learning about threads, mutex, and semaphores, this example was most useful.

Consider a bank where people line up to talk to the teller. Consider the mutex as the gate allowing a person to move from the line to the teller window. Consider the initialization to occur when the bank first opens before the first customer walks in.

If you have a teller already at the window, initialize the mutex to "open" so that the first customers doesn't have to wait at all (their first call to WaitForSingleObject() immediately succeeds).

If the teller is doing something else, initialize the mutex to "closed" so that the first customer has to wait.

Each time the teller is ready to serve another customer, they "signal" the mutex / event to allow a wait to be satisfied.

So, the WaitForSingleObject() is simply the way you know you can proceed from the line to the teller window. The initialization of the mutex is how you define how the first customer's wait is satisfied.

It is an exercise for the student to modify the above example to use other objects and/or maximum values to allow for multiple tellers to service the single line of waiting customers (Single Queue, Multi Server Queueing).
 
Share this answer
 
v2
Comments
Espen Harlinn 13-Oct-11 17:27pm    
Good example :)
Sergey Alexandrovich Kryukov 30-Mar-12 21:53pm    
Yes, nice explanation, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900