I've got a Queue<T> _q = new Queue<T>();
There's always only one thread populating it: _q.Enqueue(msg);
There's always only one thread consuming it: _q.Dequeue();
I want to be lock-free in both threads. Performance is important but safety is on the first place.
Do I need making _q
a ConcurrentQueue<T>
?
UPD
That can be important:
I do not need to be sure that reading thread will successfully dequeue item immediatly after writing thread enqueues it. If a few false
dequeues will be taken - that's ok.
Do I need making _q a ConcurrentQueue
Yes. A Queue is not thread safe.
Here you can find a lock free queue: Lockfreie threadsichere Queue - don't be afraid of the site being in german - the code should speak for itself. Furthermore, there's another, little more in-depth article at Writing Lock-Free Code: A Corrected Queue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With