Synchronized Producer-Consumer problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Synchronized Producer-Consumer problem

  1. #1
    Join Date
    Dec 2010

    Synchronized Producer-Consumer problem

    If I let the producer (one producer) produce at its will, as fast or slow as it can (But with timestamps the consumers need to follow)
    But 2 or more consumers must consume the products at regular intervals...
    Say at time 0, they must consume the correct items....
    and at time 1, they must consume the items with the correct timestamps.
    If the producer is too slow to catch up, the consumers just pause.
    If the consumers are too slow, I will leave the container have as many items as it can hold in the buffer
    But the consumers cannot skip timestamps, they must consume sequentially
    How can I achieve this?
    Last edited by luckiejacky; April 16th, 2017 at 05:50 AM.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Synchronized Producer-Consumer problem

    Will a queue not work? The producer pushes and the consumer pops? See
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain

    C, C++ Compiler: Microsoft VS2017

  3. #3
    Join Date
    Feb 2017

    Re: Synchronized Producer-Consumer problem

    Quote Originally Posted by luckiejacky View Post
    How can I achieve this?
    The easiest solution probably is to let the producer communicate individually with each consumer. The producer could maintain one P-C queue for each of the consumers who all run in their own threads. The producer pushes the same messages on all P-C queues and the consumers pop them whenever they like, say at certain time-intervals. In this way each costumer gets its own copy of the exact same information so there will be no interference between consumers to consider. They can all act independently.

    This involves multi-threading so synchronization will be necessary. Easiest and safest would be to use a concurrent-queue implementation from some library. If you are using MS technology there is one in the PPL. You may also want to check out the Intel TBB.

    Here's a simple implementation I use myself (adapted from C++ Concurrency in action by Anthony Williams page 154). It has worked fine so far but I can give no guarantees of course.

    #include <queue>
    #include <memory>
    #include <condition_variable>
    #include <mutex>
    	template<typename ITEM>
    	class Blocque {
    		using Mutex = std::mutex;
    		void push(ITEM value) { // push
    			std::lock_guard<Mutex> lock(mutex);
    		bool try_pop(ITEM& value) { // non-blocking pop
    			std::lock_guard<Mutex> lock(mutex);
    			if (queue.empty()) return false;
    			value = std::move(queue.front());
    			return true;
    		ITEM wait_pop() { // blocking pop
    			std::unique_lock<Mutex> lock(mutex);
    			condition.wait(lock, [this]{return !queue.empty(); });
    			ITEM const value = std::move(queue.front());
    			return value;
    		int size() { // queue size
    			std::lock_guard<Mutex> lock(mutex);
    			return static_cast<int>(queue.size());
    		Mutex mutex;
    		std::queue<ITEM> queue;
    		std::condition_variable condition;
    Last edited by wolle; April 18th, 2017 at 03:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This a survey!

HTML5 Development Center