0

The code should be written in C++. I'm mentioning this just in case someone will suggest a solution that won't work efficient when implementing in C++.

Objective:

Producer that runs on thread t1 inserts images to Consumer that runs on thread t2. The consumer has a list of clients that he should send the images to at various intervals. E.g. client1 requires images every 1sec, client2 requires images every 5sec and etc.

Suggested implementation:

There is one main queue imagesQ in Consumer to which Producer enqueues images to. In addition to the main queue, the Consumer manages a list of vector of queues clientImageQs of size as number of clients. The Consumer creates a sub-consumer, which runs on its own thread, for each client. Each such sub-consumer dequeues the images from a relevant queue from clientImageQs and sends images to its client at its interval.

Every time a new image arrives to imagesQ, the Consumer duplicates it and enqueus to each queue in clientImageQs. Thus, each sub-consumer will be able to send the images to its client at its own frequency.

Potential problem and solution:

If Producer enqueues images at much higher rate than one of the sub-consumers dequeues, the queue will explode. But, the Consumer can check the size of the queue in clientImageQs before enqueuing. And, if needed, Consumer will dequeue a few old images before enqueuing new ones.

Question

Is this a good design or there is a better one?

theateist
  • 13,879
  • 17
  • 69
  • 109
  • Should every client be getting the same image? – NathanOliver Sep 26 '18 at 21:37
  • @theateist I dunno? Because adding irrelevant tags? Wasn't me at least. – πάντα ῥεῖ Sep 26 '18 at 21:37
  • @NathanOliver, can you elaborate what you mean? Let's assume images arrive to the main queue `imagesQ` every second, client1 requires the images every 1sec and client2 requires images every 5 sec. Therefore, client1 should get every image and client2 should get every 5th image. Meaning, when the thread of client2 wakes up to send the image it should send the 5th image. Now that I'm writing I realise that I didn't take this into account in my implementation. Any thoughts are appreciated. – theateist Sep 26 '18 at 21:49

1 Answers1

0

You describe the problem within a set of already determined solution limitations. Your description is complex, confusing, and I dare say, confused. Why have a consumer that only distributes images out of a shared buffer? Why not allow each "client" as you call it read from the buffer as it needs to? Why not implement the shared buffer as a single-image buffer. The producer writes at its rate. The clients perform non-destructive reads of the buffer at their own rate. Each client is ensured to read the most recent image in the buffer whenever the client reads the buffer. The producer simply over-writes the buffer with each write. A multi-element queue offers no benefit in this application. In fact, as you have described, it greatly complicates the solution. See http://sworthodoxy.blogspot.com/2015/05/shared-resource-design-patterns.html Look for the heading "unconditional buffer". The examples in the posting listed above are all implemented using Ada, but the concepts related to concurrent design patterns are applicable to all programming languages supporting concurrency.

Jim Rogers
  • 4,822
  • 1
  • 11
  • 24