Syncronizing two threads

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
 
R

rowe_newsgroups

How can I tell the consumer that data
is available, without running useless circles in an do...loop?

Why not just raise an Event when the data is available?

Thanks,

Seth Rowe
 
A

Armin Zingler

Iason Mavip said:
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

First, it's strongly recommended to switch Option Strict On.

Untested(!) solution:

Class Jobs
Public Event ItemAdded()
Public Event DoneChanged()

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
RaiseEvent DoneChanged()
End Set
End Property

Public Sub Add(ByVal n As Integer)
o.Enqueue(n)
RaiseEvent ItemAdded()
End Sub

Public Function Fetch() As Integer
If o.Count > 0 Then
Return o.Dequeue
Else
Return 0
End If
End Function

End Class

Class Producer

Public Sub Produce(ByVal q As Jobs)

For i As Integer = 1 To 100
SyncLock q
q.Add(i)
End SyncLock
Threading.Thread.Sleep(100)
Next

q.Done = True

End Sub

End Class

Class Consumer

Private ARE As New Threading.AutoResetEvent(False)

Public Sub Comsume(ByVal q As Jobs)

AddHandler q.ItemAdded, AddressOf OnItemAdded
AddHandler q.DoneChanged, AddressOf OnDoneChanged

Do
ARE.WaitOne()

Do Until q.Done
Dim value As Integer
SyncLock q
value = q.Fetch
End SyncLock
Debug.Print(value.ToString)
If value = 0 Then Exit Do
Loop
Loop Until q.Done

End Sub
Private Sub OnItemAdded()
ARE.Set()
End Sub
Private Sub OnDoneChanged()
ARE.Set()
End Sub

End Class


The AutoResetEvent is the key. It waits til the queue is "done" or an item
has been added w/o CPU usage.

Are you sure that the Queue will never contain 0? Otherwise, returning
0 for an empty queue is ambiguous. I would add a Count property, or
you can Inherit from the generic Queue.

(BTW, I'd prefer stopping the Consumer processing the queue instead of
setting a Done flag in a queue, but maybe there's a reason for you doing it
this way. In addition, the Done property could be set from True to False
which wouldn't make sense.)


Armin
 
I

Iason Mavip

rowe_newsgroups said:
Why not just raise an Event when the data is available?

Thanks for your suggestion; I already thought about using an event to
spawn a new consumer thread each time data becomes available. But to
ensure data consistency I would feel a lot better if only one thread
would access the database at a time - I'm dealing with an archaic Access
database :-/
 
A

Armin Zingler

Armin Zingler said:
Public Sub Comsume(ByVal q As Jobs)

AddHandler q.ItemAdded, AddressOf OnItemAdded
AddHandler q.DoneChanged, AddressOf OnDoneChanged

Do

To make it bullet-proof, this must be

Do Until q.Done
ARE.WaitOne()

Do Until q.Done
Dim value As Integer
SyncLock q
value = q.Fetch
End SyncLock
Debug.Print(value.ToString)
If value = 0 Then Exit Do
Loop
Loop Until q.Done

Loop

Because, theoretically, the Producer might be ready and has set q.done =
true before the Consumer has attached the event handlers, so the Consumer
would wait forever. (one reason why other applications might hang or crash
only every 100st time and nobody knows why).


Armin
 
C

Cor Ligthert[MVP]

Iason,

Why are you using an assynchonized process, when you need a synchronised
proces?

Cor
 
A

Armin Zingler

Cor Ligthert said:
Iason,

Why are you using an assynchonized process, when you need a
synchronised proces?

Why do you think this? "A synchronized process" is a contradictive term in
itself. To synchronize, you have to have at least two processes (as you
know). You probably assume that putting one item in the queue has to wait
until the previous item has been processed, but this would blocks the thread
(the Producer class). I guess this is what the OP wants to avoid.


Armin
 
C

Cor Ligthert[MVP]

Armin,

I am more curious why somebody would create a two processes for getting and
consuming data, while that is most often a very serialized process. (This
beside the situations where the periphery is not as downloading using
yourself slow lines while the provider is giving it to you very fast).

In my idea in a serialized process you know that there is data available to
consume while in a paralyzed process you need to do extras, while the effort
will be that the processor will be (probably not visible) slower.

However there can be a reason I don't know. The one you gave me is obvious,
however I always want to know the why. I come from a not so discipliner
culture you know.

:)

Cor
 
A

Armin Zingler

Cor Ligthert said:
I come from a not
so discipliner culture you know.

:)

Even with your smiley, I don't know what this (quoted) nonsense should tell
me.


Armin
 
G

Guest

Thanks for your suggestion; I already thought about using an event to
spawn a new consumer thread each time data becomes available. But to
ensure data consistency I would feel a lot better if only one thread
would access the database at a time - I'm dealing with an archaic Access
database :-/

You could lock the record using status flags?
 
A

Anon-E-Moose

Even with your smiley, I don't know what this (quoted) nonsense should
tell me.

I find it hard to understand alot of Cor's comments too... Most of the time
it's jibberish to me.
 
C

Cor Ligthert[MVP]

That I try to find out why somebody does something, not only answering on
the technical problem he has.

Sometimes we (including me) thinks that we are improving things, however we
are sometimes wrong in the way we are thinking that it goes.

This is especially with multithreading. A processor or channel is mostly
busy, even if it a processor with mutltithreading build in. It has no sense
to break that up in more processes as long as there are no external wait
times. which can overcome as there is no serial wait time for the user
involved.

I have the idea that in this question this is the problem.

Cor
 
A

Armin Zingler

Cor Ligthert said:
I have the idea that in this question this is the problem.

Please read what I quoted and what I replied. It was not about
multithreading at all. I was referring to you term "not so discipliner
culture". I don't know what this should mean.


Armin
 
C

Cor Ligthert[MVP]

"> Please read what I quoted and what I replied. It was not about
multithreading at all. I was referring to you term "not so discipliner
culture". I don't know what this should mean.
That I feel free to write what I want in this forum as long as it is decent
and including the vb.net language. Whatever others thinks that it should be.

Cor
 
A

Armin Zingler

Cor Ligthert said:
"> Please read what I quoted and what I replied. It was not about
That I feel free to write what I want in this forum as long as it is
decent and including the vb.net language. Whatever others thinks
that it should be.

Of course, you can write whatever you want. I only was trying to understand
it.


Armin
 
B

Brian Gideon

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

See the following article for a correct implementation of the producer-
consumer pattern. Unfortunately, the example is in C#.

http://www.yoda.arachsys.com/csharp/threads/deadlocks.shtml
 
I

Iason Mavip

Brian said:
See the following article for a correct implementation of the producer-
consumer pattern. Unfortunately, the example is in C#.

Thanks for the link, I guess that's exactly what I was looking for.
Oh, and C# shouldn't be a problem.

Iason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top