Synchronised queue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason

    #1

    Synchronised queue

    I want to create a synchronised queue class which has very simple
    functionality as a queue but is thread safe, where two different threads can
    add and remove items in such a way that doesn't interfere with each other.

    Will the following code suffice. Does the synchronised method return a queue
    that can only be accessed by a single thread at any one time.

    Also what happens if you try and remove (dequeue) an item when the queue is
    empty. Will the thread be blocked until a new item is added to the queue.

    Effectively what I am trying to do, is a single producer - single consumer
    solution (which fits the requirements fine) without getting into all the
    complexities related to ReaderWriter locks etc.


    TIA

    Jason

    Public Class MyMessageQueue

    Dim synchQueue As Queue = Queue.Synchroni zed(New Queue)

    Public Sub Add(ByVal strMessage As String)
    synchQueue.Enqu eue(strMessage)
    End Sub

    Public Function Remove() As String
    Return synchQueue.Dequ eue
    End Function

    Public Function Count() As Long
    Return synchQueue.Coun t
    End Function

    End Class


Working...