Solved

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

Posted on 2008-10-28
3
319 Views
Last Modified: 2008-10-29
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

0
Comment
Question by:jeaguado
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22827271
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.
0
 

Author Comment

by:jeaguado
ID: 22829240
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?
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22830076
Should have same effect.  The point is you are synchronizing the access to m_smplQueue.  What the latter will do in cases where you don't want to lock while reading, you can use special locking objects that allow write once / read multiple type of locks.

Not sure if I would call it better, but a lot of personal style goes into programming sometimes and being a developer in multiple syntaxes, I tend to use SyncLock because it is close in concept to synchronized { } blocks I use in other languages.

Here is probably better explaination:
http://www.developerfusion.com/article/5184/multithreading-in-vbnet/2/
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question