Synchronize access to circular Integer buffer : Producer and consumer « Thread « VB.Net Tutorial






Imports System.Threading

Public Class Tester
   Public Shared Sub Main
      Dim buffer As New Buffer()
      Dim producer As New Producer(buffer)
      Dim consumer As New Consumer(buffer)
      Dim producerThread As New Thread(AddressOf producer.Produce)
      Dim consumerThread As New Thread(AddressOf consumer.Consume)

      producerThread.Name = "Producer"
      consumerThread.Name = "Consumer"

      producerThread.Start()
      consumerThread.Start()
   End Sub 

End Class 


Public Class Buffer
   Private mBuffer As Integer() = {-1, -1, -1}

   Private occupiedBufferCount As Integer

   Private readlocation, writeLocation As Integer

   Property Buffer() As Integer
      Get
         SyncLock (Me)

            If occupiedBufferCount = 0 Then
               Console.WriteLine("All buffers empty. " & _
                  Thread.CurrentThread.Name & " waits.")
               Monitor.Wait(Me)
            End If

            Dim readValue As Integer = mBuffer(readlocation)

            Console.WriteLine(Thread.CurrentThread.Name & " reads " & _
               mBuffer(readlocation))

            occupiedBufferCount -= 1

            readlocation = (readlocation + 1) Mod mBuffer.Length

            Monitor.Pulse(Me)
            Return readValue
         End SyncLock

      End Get

      Set(ByVal Value As Integer)
         SyncLock (Me)
            If occupiedBufferCount = mBuffer.Length Then
               Console.WriteLine("All buffers full. " & _
                  Thread.CurrentThread.Name & " waits.")
               Monitor.Wait(Me)
            End If

            mBuffer(writeLocation) = Value

            Console.WriteLine(Thread.CurrentThread.Name & " writes " & _
               mBuffer(writeLocation) )

            occupiedBufferCount += 1
            writeLocation = (writeLocation + 1) Mod _
               mBuffer.Length

            Monitor.Pulse(Me)
         End SyncLock

      End Set

   End Property ' Buffer

End Class 


Public Class Producer
   Private buffer As Buffer

   Public Sub New(ByVal sharedObject As Buffer)
      buffer = sharedObject
   End Sub 
   Public Sub Produce()
      Dim count As Integer

      For count = 1 To 10
         Thread.Sleep(1000)
         buffer.Buffer = count
      Next

      Console.WriteLine(Thread.CurrentThread.Name & _
         " done producing. " & vbCrLf & _
         Thread.CurrentThread.Name & " terminated.")
   End Sub 

End Class 

Public Class Consumer
   Private buffer As Buffer

   Public Sub New(ByVal sharedObject As Buffer)

      buffer = sharedObject
   End Sub 
   Public Sub Consume()
      Dim count, sum As Integer

      For count = 1 To 10
         Thread.Sleep(500)
         sum += buffer.Buffer
      Next

      Console.WriteLine("Total " & _
         Thread.CurrentThread.Name & " consumed: " & sum & vbCrLf & _
         Thread.CurrentThread.Name & " terminated.")

   End Sub

End Class
All buffers empty. Consumer waits.
Producer writes 1
Consumer reads 1
All buffers empty. Consumer waits.
Producer writes 2
Consumer reads 2
All buffers empty. Consumer waits.
Producer writes 3
Consumer reads 3
All buffers empty. Consumer waits.
Producer writes 4
Consumer reads 4
All buffers empty. Consumer waits.
Producer writes 5
Consumer reads 5
All buffers empty. Consumer waits.
Producer writes 6
Consumer reads 6
All buffers empty. Consumer waits.
Producer writes 7
Consumer reads 7
All buffers empty. Consumer waits.
Producer writes 8
Consumer reads 8
All buffers empty. Consumer waits.
Producer writes 9
Consumer reads 9
All buffers empty. Consumer waits.
Producer writes 10
Producer done producing.
Producer terminated.
Consumer reads 10
Total Consumer consumed: 55
Consumer terminated.








23.6.Producer and consumer
23.6.1.Producer and consumer
23.6.2.Producer and consumer without synchronization mechanisms
23.6.3.Producer and consumer with synchronization mechanisms
23.6.4.Synchronize access to circular Integer buffer