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

pthread segfault doing parallel i/o

I'm having an issue regarding some parallel I/O I've been attempting to do. The output response from the code varies depending on the run. Essentially, I'm creating a producer/consumer program that receives as input one directory and completely…
0
votes
2 answers

Communication between GUI and persistent backend (running in separate thread)

Hello I know there is many questions about it and some relevant answers. Like for returning response into GUI JTextArea from backend use SwingUtilities.invokeLater and for passing messages to backend using blocking queue. This I can work with. But I…
Vit Bernatik
  • 3,566
  • 2
  • 34
  • 40
0
votes
1 answer

Implementing Producer/Consumer Communication

I am attempting to implement producer/consumer communication through a bounded buffer using semaphores and locks I've already implemented in C. I need to have the producer place "hello world" onto a 5-byte buffer, one character at a time. The…
user3068177
  • 357
  • 2
  • 5
  • 17
0
votes
2 answers

Should I implement the consumer/producer pattern in my java video app, and if yes, how?

I built a small video frame analysis app with desktop Java 8. On each frame, I extract data (5 doubles now, but could expand to a 1920x1080x3 OpenCV Mat in the future). I would like to store this data into a database (Java DB, for example) to…
Raoul
  • 1,872
  • 3
  • 26
  • 48
0
votes
0 answers

producer consumer condition variable

I read data from a file and process it in a seperate thread. I am trying to parallelize the data read and processing parts on two threads and using conditional variables with infinite loops. However, I end up with deadlocks. char…
Naveen Sharma
  • 1,057
  • 12
  • 32
0
votes
1 answer

Termination in Producer Consumer multithreading Java

I am doing some exercise with multithreading and Java concurrency features. I have 1 producer and 4 consumers. Now my questions is: Is there any other more smart way to stop the consumers when I am sure that the producer have finished to produce in…
0
votes
1 answer

pthread_mutex_wait multiple producer and consumer

I was going through this and this link. Basically, they are discussing why classical single threaded producer consumer design (with signal and wait does not work for multi-producer scenario). I have one doubt which has been bugging me - Argument…
kumar_m_kiran
  • 3,982
  • 4
  • 47
  • 72
0
votes
1 answer

An implementation of integer buffer that creates deadlock

Let’s consider the following pseudocode: class OneBuf{ Mutex m; CondVar cv; int buffer; bool full; void put(int data){ m.lock(); while(full){ cv.wait(m); } buffer = data; full…
mgus
  • 808
  • 4
  • 17
  • 39
0
votes
1 answer

Why BlockingCollection doesn't implement IProducerConsumerCollection?

I recently needed an IProducerConsumerCollection implementation but I wanted it to block on TryAdd if a certain capacity has been reached and block on TryTake if it's empty. I was certain that BlockingCollection is actually an implementation of…
Yannis
  • 6,047
  • 5
  • 43
  • 62
0
votes
1 answer

how can I stop a specific thead in semaphore in java?

I have implemented producer and consumer problem with semaphore. I need a way that when there is no product for consuming , the current thread wait until a producer produce a product. please guide me.
daneshjoo
  • 27
  • 7
0
votes
1 answer

Why kafka consumer goes infinite when maxread value is large and message reaceived is in small quantity?

I have used a kafka Simple Consumer. All i want to do is that it will get message from the producer, then this data is processed in STORM and then after that will save to Cassandra.All goes well but the problem is that whenever i increases the…
0
votes
1 answer

producer-consumer using arraydeque working only in breakpoint

I am implementing a producer and consumer pattern using ArrayDeque and running into a strange problem. Consumer.java Class Consumer { public final Queue my_queue = new ArrayDeque(); public void begin() { new Thread() { …
delita
  • 1,571
  • 1
  • 19
  • 25
0
votes
1 answer

c# which collection to use multiple producers and a single consumer?

I have a bunch of threads producing objects (e.g. strings) and a single thread consuming batch of objects (e.g. serializing them and sending yo a remote server). I want producers to be able to pish data as fast as possible. They should never lock…
Igor Gatis
  • 4,648
  • 10
  • 43
  • 66
0
votes
2 answers

Oracle connection issue while using tasks and blocking collection

I have some tasks (nWorkers = 3): var taskFactory = new TaskFactory(cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning, TaskScheduler.Default); for (int i = 0; i < nWorkers; i++) { var…
Mario Arturo
  • 347
  • 3
  • 9
0
votes
2 answers

Producer consumer in batches; second batch shouldn't come until the previous batch is complete

I'm trying to implement a mechanism where the runnables are both producer and consumer; Situation is- I need to read records from the DB in batches, and process the same. I'm trying this using producer consumer pattern. I get a batch, I process.…
karansardana
  • 1,039
  • 2
  • 9
  • 8