Link to home
Start Free TrialLog in
Avatar of jeaguado
jeaguado

asked on

I don't understand monitor.wait and monitor.pulse

I'm trying to understand this method to syncronizate in multithreading but I don't understand them.
I have found the following example but I dont' understand it.

I don't understand how it use the Queue as a lock object.

I think wait method over a lock variable wait until another process pulse that lock variable.

Could you explain me this method because it is hard to me understanding it. I have been working with VB6 and now I'm learning VB 2008 and it is very hard.

Another thing, Do you know a good book of VB2008 I could buy in Spain?

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

Open in new window

Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

You are creating lock on the object that you whose changes/reads you want to make thread safe.  Meaning, you lock m_smplQueue while it is being incremented so that you don't get two threads trying to increment from 3 to 4 as an example.  You want 3 to go to 4 in thread 1 then thread 2 to further increment to 5.  Likewise when reading, you don't want to read 3 and act on that amount of data in m_smplQueue when really there is about to be 4.

Not sure if that made it any clearer, but that is my attempt.

It is like application lock when setting application variable in VB6 (ASP).  You could have technically locked the entire MonitorSample instance (application) until you were done changing the value of its member m_smplQueue but that would then also prevent you from changing value in another member like m_smplQueue2.
Avatar of jeaguado
jeaguado

ASKER

In this example, Could I use another object only defined to be used by wait and pulse?
Dim m_objLock as object

Is better using monitor.enter than syncLock?
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial