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

disruptor producer too fast for consumer

I am using a disruptor for some business logic which publishes to another disruptor that handles IO. The events published to the IO disruptor can arrive too fast to construct and validate the IO. Well, that's kind of the point... The IO disruptor is…
rupweb
  • 3,052
  • 1
  • 30
  • 57
0
votes
0 answers

BlockingCollection Consumer is repeating output

TL;DR I have an application that is reading messages from a USB device in the background, and displaying the messages on the screen. I am using a BlockingCollection, as I need to read messages quickly so the device does not get a BufferOverflow. I…
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
0
votes
0 answers

c++ Producer Consumer Code Design

I already have a design for my Producer Consumer problem with restrictions. struct data{ int id; int value; }; std::list g_DataQ; class Consumer{ void process_data(){ struct data my_data = g_DataQ.pop_front(); …
Rachit Agrawal
  • 3,203
  • 10
  • 32
  • 56
0
votes
0 answers

Java heap space producer consumer

I'm learning to implement producer consumer problem using wait and notify methods. I'm using a linked list, and set limit to 20 items for the list. Below is the code: import java.util.LinkedList; import java.util.Random; public class…
gaurav jain
  • 3,119
  • 3
  • 31
  • 48
0
votes
2 answers

Kafka have both partitioning and consumer group

Can we use both partitioning and consumer group for same topic. So we want to create a topic which have partitioning and then create multiple consumers to it and out of them 2-3 will be generic listening to all messages(needs to be in a consumer…
Abhishek bhutra
  • 1,400
  • 1
  • 11
  • 29
0
votes
1 answer

Single producer multiple consumer buffer pthread implementation using condition variables

I'm writing a naive implementation of a single producer multiple consumer buffer with pthreads and condition variables, using C++ list as a buffer. I'm not too much worried about how fast my code runs, I just want to get rid of the errors. The…
0
votes
2 answers

Dead Lock In a Single Producer Multiple Consumer Case

Could anyone point out why this code can cause dead-lock? It is a single producer, multiple consumer problem. The producer have 8 buffers. Here it has 4 consumers. Each consumer will have two buffers. When a buffer is filled, it flags it to be ready…
Jack
  • 1
0
votes
0 answers

producer consumer using msdn libraries in C++

I am trying to re-write the classic producer-consumer algorithm using Windows libraries in C++. The snipped below was copied from a Java sample. Anyone know the equivalent of lock.notify and lock.wait using Windows Libraries such as…
gmmo
  • 2,577
  • 3
  • 30
  • 56
0
votes
1 answer

stopping threads that run indefinitely

I have been trying to implement the producer consumer pattern. If both producer and consumer are running indefinitely how should one try to stop them? I have been trying to test the status of isInterrupted() but the following code does not guarantee…
0
votes
3 answers

Different results in run and deburg mode with Multithread producer-consumer

In a JPA project, I have two threads: producer and consumer. Producer should get the search result of twitter query from another class and put it in a LinkedBlockingQueue, thread consumer should consume the result and use another class to persist…
jd466
  • 547
  • 1
  • 6
  • 20
0
votes
0 answers

Rabbitmq publish exception

I am new to Rabbitmq and just doing producer-consumer implementation below is my producer which fetches data from API and push to queue, so problem is that for some time everything works fine but suddenly it throws exception (after 20-24 hours) and…
Yatender Singh
  • 3,098
  • 3
  • 22
  • 31
0
votes
1 answer

Consumer-Producer with Threads and BlockingQueues

I wrote a Class 'Producer' which is continuously parsing files from a specific folder. The parsed result will be stored in queue for the Consumer. public class Producer extends Thread { private BlockingQueue queue; ... public…
mr.proton
  • 993
  • 2
  • 10
  • 24
0
votes
3 answers

Conditional Go Routine/Channel

I would like to make the statistics routine conditional so that it only runs on certain cases otherwise it will waste cycles half the time. Right now I have one go routine act as a producer to feed the two consumer routines via buffered channels. Is…
pdago
  • 107
  • 2
  • 9
0
votes
1 answer

end condition for producer-consumer model

I am learning Pthread programming. Here is the question i got at the end of my assignment: I want to copy each bytes in the source file to a new .txt file by using producer-consumerproblem. Here is my code structure: void *producer(....) { …
Rob Ye
  • 63
  • 1
  • 7
0
votes
2 answers

How to implement one producer , multiple consumers and multiple Objects in Java?

I have a question about Producer/consumer Design pattern , in fact my situation is :I have One class that produce multiple types of messages (notifications) and multiple consumers who consume those messages. The complication is my Producer produce…
Zakaria
  • 1,675
  • 4
  • 21
  • 23