Thursday, February 10, 2005

Mutex - 1

Mutex
Mutex are like Monitors in that they ensure only one thread can execute a given section of the code between the (assumming that a Mutex object has been created called MutexObject) MutexObject.WaitOne() and MutexObject.ReleaseMutex() .

The default mutex constructor will create a mutex that isn’t owned by the calling thread. That means it is signalled and any thread that has the MutexObject.WaitOne() will not block.

If the mutex constructor is passed a (true) argument, then the calling thread has ownership and All the other thread that is waiting (MutexObject.WaitOne() ) on the mutex will be blocked. They can be unblocked only when the calling thread calls MutexObject.ReleaseMutex().

When the MutexObject.WaitOne() is called, it waits on the MutexObject if it doesn’t own the mutex. i.e., if the MutexObject is not owned, then the call is blocked. Otherwise, the call to MutexObject.WaitOne() gets through.

When the thread that owns the MutexObject wants to release its ownership (i.e. allows other threads that are waiting on the MutexObject) it calls the MutexObject.ReleaseMutex() method. Care must be ensured that only the owning thread can call the ReleaseMutex() method, otherwise an ApplicationException exception will be thrown.

0 Comments:

Post a Comment

<< Home