Tip - It should specify FALSE as bInitialOwner parameter while creating mutex by CreateMutex API if it is using for synchronizing the processes.
Details - 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. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.
Details - 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. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.
Interested people can experiment the behavior change when it set TRUE to bInitialOwner flag by referring the below steps & attached simple application.
1. Run two instance of the attached application(name it as App1 and App2)
2. Click Start button in App1 (it will acquire lock)
3. Click Start button in App2 (App2 will wait for acquiring the lock. it is expected to wait until the App1 release the lock)
4. Click End button in App1( it will release the lock. We expecting other application can acquire the lock). Try to click within 7 sec. (7 Sec is given as the timeout value in WaitForSingleObject).
You can realize that the App2 will never acquire the lock.
Refer the code CSYNCHDlg::OnStart() and modify CreateMutex call with FALSE and try again. It will work as we expecting!
Reference -
http://msdn.microsoft.com/en-us/library/ms682411%28VS.85%29.aspx
Posted by - Ajmal Karuthedath
1. Run two instance of the attached application(name it as App1 and App2)
2. Click Start button in App1 (it will acquire lock)
3. Click Start button in App2 (App2 will wait for acquiring the lock. it is expected to wait until the App1 release the lock)
4. Click End button in App1( it will release the lock. We expecting other application can acquire the lock). Try to click within 7 sec. (7 Sec is given as the timeout value in WaitForSingleObject).
You can realize that the App2 will never acquire the lock.
Refer the code CSYNCHDlg::OnStart() and modify CreateMutex call with FALSE and try again. It will work as we expecting!
Reference -
http://msdn.microsoft.com/en-us/library/ms682411%28VS.85%29.aspx
Posted by - Ajmal Karuthedath
No comments:
Post a Comment