Windows api createmutex


















If the function fails, the return value is zero. To get extended error information, call GetLastError. A thread obtains ownership of a mutex either by creating it with the bInitialOwner parameter set to TRUE or by specifying its handle in a call to one of the wait functions.

When the thread no longer needs to own the mutex object, it calls the ReleaseMutex function so that another thread can acquire ownership. A thread can specify a mutex that it already owns in a call to one of the wait functions without blocking its execution.

This 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 one time for each time that it obtained ownership either through CreateMutex or a wait function.

In windows, Mutex is a synchronization object. It helps to synchronize threads in multithreading programming. In windows , Mutex API provides the name of mutex. With the help of this we can open the mutex in other program and synchronize the threads.

It also works in same program. Suppose we have created 2 separate programs, Test1. In Test1. We can open this named object in Test2. Programming Steps: 1.

CreateMutex — Creates or opens a named or unnamed mutex object. Ex — represent Extended. CreateMutex Parameters 1. Default value is NULL. If TRUE — calling thread must have initial ownership of mutex. The creating thread can use the bInitialOwner flag to request immediate ownership of the mutex. Otherwise, a thread must use one of the wait functions to request ownership. When the mutexs state is signaled, one waiting thread is granted ownership, the mutexs state changes to nonsignaled, and the wait function returns.

Only one thread can own a mutex at any given time. The owning thread uses the ReleaseMutex function to release its ownership. 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. 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. Multiple processes can have handles of the same mutex object, enabling use of the object for interprocess synchronization.



0コメント

  • 1000 / 1000