Questions tagged [producer-consumer]

The Producer-Consumer Problem (also known as the bounded-buffer problem) is a classical example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.

The Producer-Consumer Problem (also known as the bounded-buffer problem) is a classical example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate a piece of data, put it into the buffer and start again. At the same time the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer.

The solution for the producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached by means of inter-process communication, typically using semaphores. An inadequate solution could result in a deadlock where both processes are waiting to be awakened. The problem can also be generalized to have multiple producers and consumers.

References

1564 questions
0
votes
1 answer

consumer-producer pattern with MySql

Hello and happy New Year to all of you! :) I'm working on a software (Java, backend) that has 3 components each being independent Java application (you can even consider them as separate threads): 1) DB data importer 1. 2) DB data importer 2. 3) DB…
guest86
  • 2,894
  • 8
  • 49
  • 72
0
votes
2 answers

C++: condition-variable wait

#include #include #include #include std::mutex globalMutex; std::condition_variable globalCondition; int global = 0; int activity = 0; int CountOfThread = 1; // or more than 1 // just for console…
0
votes
2 answers

How to control simultaneous access to multiple shared queues by multiple producers?

One way would be to lock and then check the status of first shared queue, push data if space available, or ignore if not, and then unlock. Then check the status of second shared queue, push data if space available, or ignore if not, and then…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
0
votes
2 answers

Multiple producers and consumers but one shared reource - Only one thread is running

Here I have created 2 producer threads and 2 consumer threads. They put and take out values only in and from one shared queue. Problem is that first producer does fill in and then gets into waiting mode. After that no other thread runs. Explain…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
0
votes
2 answers

Producer/Consumer pattern with BlockingQueue and return value or exception

I'm working on a producer/consumer pattern, my producer should wait the answer that can be a result object or an exception. What is the best way to do so? I read many examples of this pattern but every time the producer never minds about a return…
Tobia
  • 9,165
  • 28
  • 114
  • 219
0
votes
0 answers

Enhanced Producer-Consumer (minimum items for a consumer) using semaphores

I'm solving a producer-consumer problem with additional conditions using semaphores. For example: consumer cannot read from the buffer until there are at least 3 elements in it. My question is: are conditions like these easily expressible using only…
0
votes
1 answer

homework, java, Avoiding deadlock when using notify() instead of notifyAll() on multiProducer/singleConsumer model

so I'm working on this report, and the last assignment is to create a producer/consumer model with n Producers and one Consumer, the trick is that i have to make it work with notify() instead of notifyAll() in the put and get methods. I figure just…
0
votes
0 answers

Polling from ConcurrentLinkedQueue until time quantum expires

Suppose I want my 20 consumers to remove elements from ConcurrentLinkedQueue until their time quantum expires.That is i want each consumer to just run only once until its time slice reaches and then die. Following is my code but it is not working…
0
votes
1 answer

java multithread: How to know when a consumer thread should remove an element from queue

I have a producer consumer model which an arduino generates packets and inside my pc I have a java program which takes those packets and puts them into a BlockingQueue. Now there are threads that process these packets. Let's say X is producer and A,…
artronics
  • 1,399
  • 2
  • 19
  • 28
0
votes
0 answers

variant of minimum cost deadlock resolution

I have list of tasks that needs to be executed . Each task is a producer that updates multiple entities in response to consuming multiple entities. So tasks are dependent on each other for their execution. However, some producers are ok with…
0
votes
1 answer

Access non static method from outside of class in Java (Android app)

i am doing android app in which i am doing producer consumer problem. I have following code: package nu.hci.codemenao; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Resource { public Queue
yerassyl
  • 2,958
  • 6
  • 42
  • 68
0
votes
2 answers

Even ,odd and evenodd consumer- producer

What I want to do here is to print even numbers by even consumers, odd numbers by odd consumers. There is an evenodd method which is basically consuming any number and printing (whether even or odd). I have got 2 even consumer threads, 2 odd…
0
votes
0 answers

TPL Dataflow Producer/Consumer with BroadcastBlock - tracking execution

Here's my scenario: multiple producers, single consumer. The consumer works synchronously, and should only process the last input (even abort processing of what is currently being processed and start with new input if any). So I've connected a…
user3566056
  • 224
  • 1
  • 12
0
votes
0 answers

Consumer delay with producer-consumer pattern

Problem: query1, query2, query3 are added to queue. Consumer starts consuming the added query's right away. Everything works fine. After queue is empty, same query's are added to queue again. For some reason consumer now hangs for some time (5-10…
user2126173
  • 83
  • 1
  • 2
  • 8
0
votes
2 answers

Is there a faster way to print to a TextBox than AppendText?

I have a background thread that is reading messages from a device and formatting them and passing them to the 'Consumer' node of a Producer/Consumer collection that then prints all of the messages to the string. The problem I am running into is that…
AdamMc331
  • 16,492
  • 10
  • 71
  • 133