Solved

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

Posted on 2008-10-28
3
284 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
  • 2
3 Comments
 
LVL 59

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 59

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now