Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast and Best Producer/consumer queue technique BlockingCollection vs concurrent Queue [closed]

Im using Generic.Queue in C# 3.0 and Monitor.Enter,wait,exit for wait before consuming the queue (wait for the element to be enqueued). Now im moving to C# 4.

Can anyone suggest me which one is fast and best especially to avoid locks..

BlockingCollection vs concurrentQueue or any thing else...

Note. I dont want to restrict my producer

Thanks in advance..

like image 601
C-va Avatar asked Feb 15 '11 07:02

C-va


People also ask

Is BlockingCollection thread-safe?

BlockingCollection<T> is a thread-safe collection class that provides the following features: An implementation of the Producer-Consumer pattern.

How does a concurrent queue work?

What is a concurrent queue? A concurrent queue allows us to execute multiple tasks at the same time. Tasks will always start in the order they're added but they can finish in a different order as they can be executed in parallel. Tasks will run on distinct threads that are managed by the dispatch queue.


1 Answers

BlockingCollection and ConcurrentQueue are there for precisely this reason. I doubt that you'll find anything better, or simpler to use. The parallel extensions team know their stuff :)

Just a quick check on versions though - you're definitely using .NET 4, not just C# 4? (For example, you could be using Visual Studio 2010 and thus C# 4, but still targeting .NET 3.5, in which case you couldn't use Parallel Extensions.)

You may also want to start researching Task-Based Asynchronous Pattern, TPL Dataflow and the async/await features of C# 5... obviously you can't use them just yet, but it doesn't hurt to know what's coming up.

like image 56
Jon Skeet Avatar answered Oct 13 '22 18:10

Jon Skeet