I want to emphasize the “3.5” in the question. I am aware that a lot of this stuff has changed in 4.0, but I do not yet have access to this.
I have a TCP client class that takes data from another application and puts it in a Queue, and the UI thread periodically calls a function to get data from it. Currently, I do a lock on the Queue while accessing it from both sides, and I’m getting problems when too much data comes in over the network.
I figure I could implement some sort of circular buffer to get this to work, but I’m assuming this can already be done using some .NET class.
A
Queuewith a lock seems like a reasonable approach, and I wouldn’t expect performance problems as long as you are only holding the lock for the call to theQueuemethod (writer side) orDequeuemethod (reader side). Are you sure the lock is the issue?Note that a
ReaderWriterLockas suggested in @Jon B’s answer doesn’t really help since you only have one reader, and in any case you would need a write lock to callDequeue.The only case where a
ReaderWriterLockmight help would be if you had multiple reader threads calling thePeekmethod – for which they would only need a read lock.