Help! Multithreading for a noob

neodyn55

New member
Joined
Mar 1, 2008
Messages
2
Programming Experience
1-3
Ok - so I'm writing this little application for a homework problem. Here's the gist:

There's a global queue object lets call it myCustomQueue. It has a private member - mQueueLength. It has three public methods: IncrementQueue() DecrementQueue() and GetQueueLen()

There's a server() function which will run on its own thread.

there's a Client() function running as a part of the main class.

So here's whats supposed to happen: the client sleeps for a random interval (1-20 ms) and increments mQueueLength. . It keeps doing this a thousand times.

The server function checks the mQueueLength every 1 ms. If its greater than zero, then the server sleeps for a random time between 1 - 9 ms. Then decrements the queue.

This happens until the client completes its thousand times.

so the main looks like this in pseudocode:

VB.NET:
{
       ' start server on its own thread.
       t = new thread (addressof server())
       t. start
        ' wait for a few ms, just to get the server up and running

        client() ' Client() function will block till completion. 
        
        t.abort ' kill the server()
        ' done
}

OK - now clearly i need to implement synclock to guard the mQueueLength. So my queue Object looks like this:

VB.NET:
public class myQueueObj

dim m_iQueueLen as integer = 0

Public sub DecrementQueue()
      SyncLock Me 
      m_iQueueLen = m_iQueuelen-1
      End SyncLock
End sub

Public Sub IncrementQueue()
    SyncLock Me
    m_iQueueLen = m_iQueueLen + 1
    End SyncLock
End SUb

Public Function GetQueueLen() as integer
    dim rtnLenVal as integer
    SyncLock Me
    rtnLenVal = m_iQueueLen 
    End SyncLock
return rtnLenVal
End Function

End Class

So - what i want is: (1) Client will only call IncrementQueue (2) Server will call DecrementQueue() and GetQueueLen()

I can't test this till tomorrow or so. Am I doing the synclocking right? Is this program threadsafe?

Thanks so much for your advice!
 
No, SyncLock prevents two threads entering the same code block at the same time.
Use Threading.Interlocked.Increment/Decrement to modify the QueueLen variable.
 
Thanks

Thanks for the reply. So the following:

VB.NET:
public Class CustomerQueue
        '> this data structure keeps track of a queue and allows for thread-safe 'adding '> of queue length

        Dim m_iQueueLen As Integer = 0

        Public Sub DecrementQueue()

            Threading.Interlocked.Decrement(m_iQueueLen)

        End Sub

        Public Sub IncrementQueue()

            Threading.Interlocked.Increment(m_iQueueLen)

        End Sub

        Public Function GetQueueLen() As Integer

            Dim rtnLenVal As Integer

            SyncLock Me
                rtnLenVal = m_iQueueLen
            End SyncLock

            Return rtnLenVal

        End Function
    End Class

will work?

What if both client and the server threads have to access GetQueueLen()?

What happens if GetQueueLen() is called at the same time as IncrementQueue()?

Thanks in advance!
 
Getting the value is not a problem because Interlocked safely increments and decrements it.
 
Back
Top