Parallel Programming with Microsoft® .NET: Design Patterns for Decomposition and Coordination on Multicore Architectures (Patterns & Practices)

Category: Programming
Author: Colin Campbell, Ralph Johnson, Ade Miller, Stephen Toub
This Month Stack Overflow 1


by anonymous   2019-07-21

Drew is right, I think the ConcurrentQueue even though it sounds perfect for the job is actually the underlying data structure that the BlockingCollection uses. Seems very back to front to me too. Check out chapter 7 of this book* and it will explain how to use the BlockingCollection and have multiple producers and multiple consumers each taking off the "queue". You will want to look at the "GetConsumingEnumerable()" method and possibly just call .ToObservable() on that.

*the rest of the book is pretty average.


Here is a sample program that I think does what you want?

class Program
    private static ManualResetEvent _mre = new ManualResetEvent(false);
    static void Main(string[] args)
        var theQueue = new BlockingCollection<string>();
            .Subscribe(x => ProcessNewValue(x, "Consumer 1", 10000000));

            .Subscribe(x => ProcessNewValue(x, "Consumer 2", 50000000));

            .Subscribe(x => ProcessNewValue(x, "Consumer 3", 30000000));

        LoadQueue(theQueue, "Producer A");
        LoadQueue(theQueue, "Producer B");
        LoadQueue(theQueue, "Producer C");


        Console.WriteLine("Processing now....");


    private static void ProcessNewValue(string value, string consumerName, int delay)
        Console.WriteLine("{1} consuming {0}", value, consumerName);

    private static void LoadQueue(BlockingCollection<string> target, string prefix)
        var thread = new Thread(() =>
                                        for (int i = 0; i < 100; i++)
                                            target.Add(string.Format("{0} {1}", prefix, i));
by anonymous   2017-08-20

my own question, with answer, on stackoverflow's sister site:

I will copy the answer to avoid the need for click-through:

Quote Boris:

Parallel Programming with Microsoft .NET: Design Patterns for Decomposition and Coordination on Multicore Architectures

This is a book, I recommend wholeheartedly.

It is:

New - published last year. Means you are not reading somewhat outdated practices.

Short - about 200+ pages, dense with information. These days there is too much to read and too little time to read 1000+ pages books.

Easy to read - not only it is very well written but it introduces hard to grasps concepts in really simple to read way.

Intended to teach - each chapter gives exercises to do. I know it is always beneficial to do these, but rarely do. This book gives very compelling and interesting tasks. Surprisingly I did most of them and enjoyed doing them.

additionally, if you wish to learn more of the low-level details, this is the best resource i have found: "The Art of Multiprocessor Programming" It's written using java as their code samples, which plays nicely with my C# background.

PS: I have about 5 years "hard core" parallel programming experience, (abet using C#) so hope you can trust me when I say that "The Art of Multiprocessor Programming" rocks