Solved

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

Posted on 2008-10-28
3
302 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with LINQ and XML 10 35
Get the selected ValueMember of Combobox 5 30
HTML - Color not displaying correctly in EMAIL. 6 33
SqlServer no dupes 25 34
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 …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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