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
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