CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2010
    Posts
    121

    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?
    Thanks
    Jack
    Last edited by luckiejacky; April 16th, 2017 at 05:50 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Synchronized Producer-Consumer problem

    Will a queue not work? The producer pushes and the consumer pops? See http://www.cplusplus.com/reference/queue/queue/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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 https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    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.

    Code:
    #include <queue>
    #include <memory>
    #include <condition_variable>
    #include <mutex>
    
    
    	template<typename ITEM>
    	class Blocque {
    		using Mutex = std::mutex;
    
    	public:
    		void push(ITEM value) { // push
    			std::lock_guard<Mutex> lock(mutex);
    			queue.push(std::move(value));
    			condition.notify_one();
    		}
    
    		bool try_pop(ITEM& value) { // non-blocking pop
    			std::lock_guard<Mutex> lock(mutex);
    			if (queue.empty()) return false;
    			value = std::move(queue.front());
    			queue.pop();
    			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());
    			queue.pop();
    			return value;
    		}
    
    		int size() { // queue size
    			std::lock_guard<Mutex> lock(mutex);
    			return static_cast<int>(queue.size());
    		}
    
    	private:
    		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
  •  





Click Here to Expand Forum to Full Width

Featured