Reputation: 2381
I'm asking this related to this question: Using events on multithreading socket server
I´ve implemented a ConcurrentQueue
thinking it would be needed to avoid problems with multi-threading. When doing it I've noticed the only difference with the standard Queue
is in the Dequeue()
method, asking for an out
parameter.
It made me think the only protection is within the thread(s) dequeuing the object, is this true?
In my game I have a main thread processing my game logic then another thread per player that is doing the jobs of listening, serialising and sending data.
So my ConcurrentQueue
will be enqueued and dequeued by different threads but only one and always the same will call either enqueue or dequeue.
So I'm right thinking I'll be OK with a simple queue?
Is ConcurrentQueue
only needed when calling Dequeue()
simultaneously from multiple threads?
Upvotes: 0
Views: 2158
Reputation: 156978
Im my game i have a main thread processing my game logic then another thread per player that is doing the jobs of listening, serializing and sending data.
So you can end up the situation that at the exact time you dequeue an item, you also enqueue an item in another thread. In that case, the enqueued item might get lost due to the unsafe code in Dequeue
(specifically updating class members that can get out of sync). Also, resizing the backing array might cause items to get lost...
You need a ConcurrentQueue
.
Upvotes: 5