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:
OK - now clearly i need to implement synclock to guard the mQueueLength. So my queue Object looks like this:
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!
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!