Monitor.Pulse Methos

S

Søren M. Olesen

Hi

In the VS2005 documentation, the sample below exists. One thins that puzzle
me however, is that both threads can be indside the SyncLock-block....HOW
COME?? Isn't the whole purpose of synclock to prevent to different threads
to access resources at then same time???

TIA

Søren




Imports System
Imports System.Threading
Imports System.Collections


Namespace MonitorCS1
Class MonitorSample
Private MAX_LOOP_TIME As Integer = 1000
Private m_smplQueue As Queue


Public Sub New()
m_smplQueue = New Queue()
End Sub 'New

Public Sub FirstThread()
Dim counter As Integer = 0
SyncLock m_smplQueue
While counter < MAX_LOOP_TIME
'Wait, if the queue is busy.
Monitor.Wait(m_smplQueue)
'Push one element.
m_smplQueue.Enqueue(counter)
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)

counter += 1
End While
End SyncLock
End Sub 'FirstThread

Public Sub SecondThread()
SyncLock m_smplQueue
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)
'Wait in the loop while the queue is busy.
'Exit on the time-out when the first thread stops.
While Monitor.Wait(m_smplQueue, 1000)
'Pop the first element.
Dim counter As Integer = CInt(m_smplQueue.Dequeue())
'Print the first element.
Console.WriteLine(counter.ToString())
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)
End While
End SyncLock
End Sub 'SecondThread

'Return the number of queue elements.
Public Function GetQueueCount() As Integer
Return m_smplQueue.Count
End Function 'GetQueueCount

Public Shared Sub Main(args() As String)
'Create the MonitorSample object.
Dim test As New MonitorSample()
'Create the first thread.
Dim tFirst As New Thread(AddressOf test.FirstThread)
'Create the second thread.
Dim tSecond As New Thread(AddressOf test.SecondThread)
'Start threads.
tFirst.Start()
tSecond.Start()
'wait to the end of the two threads
tFirst.Join()
tSecond.Join()
'Print the number of queue elements.
Console.WriteLine(("Queue Count = " +
test.GetQueueCount().ToString()))
End Sub 'Main
End Class 'MonitorSample
End Namespace 'MonitorCS1
 
B

Barry Kelly

In the VS2005 documentation, the sample below exists. One thins that puzzle
me however, is that both threads can be indside the SyncLock-block....HOW
COME?? Isn't the whole purpose of synclock to prevent to different threads
to access resources at then same time???

SyncLock is implemented with Monitor. It translates somewhat like this
(please forgive my C#, I can read VB but can't write it):

Monitor.Enter(m_smplQueue);
try
{
// Code inside SyncLock
}
finally
{
Monitor.Exit(m_smplQueue);
}

You're right in that the purpose of SyncLock is to ensure only one
thread accesses the guarded resource at a time. But how does it do
that? Simple. Once one thread enters the monitor, via SyncLock (and
thus via Monitor.Enter), any other threads that try to enter will
block and wait for the monitor to be exited.

Advanced code can get extra behaviour. Two threads or more threads
operating in parallel can communicate using the Wait() and
Pulse()/PulseAll() methods.

When one thread acquires the monitor, with Monitor.Enter(), and
subsequently calls Monitor.Wait (and it can only call Monitor.Wait
while it owns the monitor, or IOW is inside the SyncLock), it "exits"
the monitor until:

Some other thread enters the monitor, then calls Pulse() or
PulseAll(). This Pulse() will wake up a thread that called Wait().
When this second thread releases the monitor, the first thread will
return from its Wait() call (assuming there are no other threads
contending for the lock).

The important things to remember are:

1) When a thread which has entered the monitor for a particular
resource calls the Wait() method, it has effectively exited the
monitor, and other threads can then enter.

2) Some other thread must call Pulse or PulseAll in order to wake up
these threads which have called Wait(), or else a timeout must be
provided.

3) Wait(), Pulse() and PulseAll() can only be called while the thread
currently owns the monitor.

(These monitors are almost identical to those found in Java,
specifically the synchronized keyword, Object.wait() and
Object.notify() / Object.notifyAll() methods, in case you're familiar
with those.)

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top