Thursday, February 10, 2005

Monitor Class

Monitor
Monitors are used to synchronize sections of code by acquiring a lock with the method. One thread gets the lock, while others wait until the lock is released. Once the lock is acquired on a code region, we can use the following methods between the Monitor.Enter() and Monitor.Exit() block.

Wait() – This method releases the lock on an object and blocks the current thread until it reacquires the lock.

Pulse() – This method notifies a thread that is waiting in a queue that there has been a change in the object's state.

PulseAll() – This method notifies all threads that are waiting in a queue that there has been a change in the object’s state.

Enter() and Exit() Methods
The Monitor methods are static and can be called on the Monitor class itself. Each object in the .NET framework has a lock associated with it that can be obtained and released so that only one thread at any time can access the object’s instance variable and methods. The .NET framework also provides a mechanism that allows it to be in a waiting state – the main reason for this mechanism is to aid aommunication between threads. Such mechanism arises when one thread enters the critical section of an object and needs a certain condition to exist and assumes that another thread will create that condition from the same critical section.

Wait – and – Pulse Mechanism
The question arises as to how this can be done. If only one thread is allowed in a critical section at any one point of time – when the first thread arrives at the critical section, no other threads can. So how will the second thread create a condition in the critical section when the first thread is already in it? This is how – If for example thread A has to get some data from the database and antoher thread (B) has to wait until all the data is received and then process the data. Thread B will enter the critical section and class the Wait() method and waits for thread A to notify it when the data arrives. When the data does arrives, A calls the Pulse() method, which notifies B so that B can process the data. This is the Wait and Pulse mechanism.

(i) The first thread enters the critical section and executes the Wait() method.
(ii) The Wait() method releases the lock prior to waiting.
(iii) The second thread is now allowed to enter the critical section, changes the requird condition, and calls the Pulse() method to notify the waiting thread that the condition has been reached and it can now continue its execution.
(iv) The first thread then reacquires the lock prior to returning from the Monitor.Wait() method and continues execution form the point where it called Monitor.Wait().

No two threads can enter the Enter() method simultaneously.

Wait() and Pulse() Mechanism
They are used for interaction between threads. When a Wait() method is issued on a object, the thread that is accessing that object waits until it gets a signal to wake up. Pulse() and PulseAll() are used for signaling to waiting threads.

Wait() and Pulse() methods can only be called within the Enter() and Exit() code block.

TryEnter() Method
The TryEnter() method of the Monitor class is similar to the Enter() method in that it tries to acquire an exclusive lock on an object. But it does not block like the Enter() method. If the thread enters successfully then the TryEnter() method returns true.

0 Comments:

Post a Comment

<< Home