I
Iason Mavip
I have got two classes: One producing data and one consuming data.
Running as two threads, the first class adds data to a job-queue, the
second one fetches data from the queue.
My problem is that the consumer thread spends most of its time waiting
for data to appear on the queue. How can I tell the consumer that data
is available, without running useless circles in an do...loop?
Thanks in advance!
Iason
Code example below:
Class Jobs
Private o As New Generic.Queue(Of Integer)
Private b As Boolean
Public Property Done() As Boolean
Get
Return o.Count = 0 And b
End Get
Set(ByVal value As Boolean)
b = value
End Set
End Property
Public Sub Add(ByVal n As Integer)
o.Enqueue(n)
End Sub
Public Function Fetch() As Integer
If o.Count > 0 Then
Return o.Dequeue
Else
Return Nothing
End If
End Function
End Class
Class Producer
Public Sub Produce(ByVal o As Object)
Dim q As Jobs = CType(o, Jobs)
For i As Integer = 1 To 100
q.Add(i)
Threading.Thread.Sleep(100)
Next
o.Done = True
End Sub
End Class
Class Consumer
Public Sub Comsume(ByVal o As Object)
Dim q As Jobs = CType(o, Jobs)
Do While Not q.Done
Debug.Print(q.Fetch)
Loop
End Sub
End Class
Running as two threads, the first class adds data to a job-queue, the
second one fetches data from the queue.
My problem is that the consumer thread spends most of its time waiting
for data to appear on the queue. How can I tell the consumer that data
is available, without running useless circles in an do...loop?
Thanks in advance!
Iason
Code example below:
Class Jobs
Private o As New Generic.Queue(Of Integer)
Private b As Boolean
Public Property Done() As Boolean
Get
Return o.Count = 0 And b
End Get
Set(ByVal value As Boolean)
b = value
End Set
End Property
Public Sub Add(ByVal n As Integer)
o.Enqueue(n)
End Sub
Public Function Fetch() As Integer
If o.Count > 0 Then
Return o.Dequeue
Else
Return Nothing
End If
End Function
End Class
Class Producer
Public Sub Produce(ByVal o As Object)
Dim q As Jobs = CType(o, Jobs)
For i As Integer = 1 To 100
q.Add(i)
Threading.Thread.Sleep(100)
Next
o.Done = True
End Sub
End Class
Class Consumer
Public Sub Comsume(ByVal o As Object)
Dim q As Jobs = CType(o, Jobs)
Do While Not q.Done
Debug.Print(q.Fetch)
Loop
End Sub
End Class