Search code examples
c#c++multithreadingproducer-consumer

Multiple producers, single consumer


I have to develop a multithreaded application, where there will be multiple threads, each thread generates custom event log which need to be saved in queue (not Microsoft MSMQ).

There will be another thread which reads log data from queue and manipulates it, with certain information to save log information into a file. Basically here we are implementing Multiple-producer, Single-consumer paradigm.

Can anybody provide suggestions on how to implement this in C++ or C#.

Thanks,


Solution

  • This kind of thing is very easy to do using the BlockingCollection<T> defined in System.Collections.Concurrent.

    Basically, you create your queue so that all threads can access it:

    BlockingCollection<LogRecord> LogQueue = new BlockingCollection<LogRecord>();
    

    Each producer adds items to the queue:

    while (!Shutdown)
    {
        LogRecord rec = CreateLogRecord(); // however that's done
        LogQueue.Add(rec);
    }
    

    And the consumer does something similar:

    while (!Shutdown)
    {
        LogRecord rec = LogQueue.Take();
        // process the record
    }
    

    By default, BlockingCollection uses a ConcurrentQueue<T> as the backing store. The ConcurrentQueue takes care of thread synchronization and, and the BlockingCollection does a non-busy wait when trying to take an item. That is, if the consumer calls Take when there are no items in the queue, it does a non-busy wait (no sleeping/spinning) until an item is available.