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
2 answers

Thread priority in Producer-Consumer program - wait() & notify()

Below I have included the Producer-Consumer program (from Java - The Complete Reference Guide Ninth Edition) which is probably a familiar example of inter-thread communication to many of you. class Q { int n; boolean valueSet = false; …
Sergio Gliesh
  • 329
  • 1
  • 2
  • 8
0
votes
1 answer

Ruby - Unexpected results using multi-threading (producer/consumer model) when iterating

NOTE: I'm choosing to use threading to resolve DNS names, but the same behavior can likely be reproduced with any type of similar operation. I am receiving unexpected results when I trying to move my (previously working) code from standard…
Kurt W
  • 321
  • 2
  • 15
0
votes
2 answers

TimeToLive in JMS Producer kills my Message

if i set the TimeToLive in my Producer, my Subscriber doesn't receive any message. I use an activeMQ V. 5.13.3 as message broker. My Producer javax.naming.Context ctx = new InitialContext(); // lookup the connection factory factory =…
Tim
  • 643
  • 3
  • 12
  • 23
0
votes
0 answers

Consumer Producer gets stuck on multi-client server

I have an issue. My producer-consumer on this chat server I've built is being stuck. Here's what it does: it gives the clients turns to send their messages, and when that random client(inside race of my computer) sends his message, all the messages…
user3312767
  • 328
  • 1
  • 5
  • 11
0
votes
2 answers

Consumer/Producer with AutoResetEvent

I have the below codes in C# for consumer and producer using AutoResetEvent, but they do not work in case having multiple producer and one consumer. The problem is that the consumer can not consume all the items in the queue. When I debug, I notice…
user3033921
  • 189
  • 1
  • 8
  • 21
0
votes
1 answer

Why does this thread never gets declared or started?

I'm implementing an image downloader with the producer-consumer model. One thread is responsible for generating (url, filename) pairs and put them in queue. I want MAX_THREADS threads to pick the pairs and start downloading. Here are my…
fatg
  • 519
  • 7
  • 23
0
votes
3 answers

Only 2 threads for invoking produce and consume?

Below is the code concerning Thread running a producer and a consumer. public class PC1 { public static final int limit = 3; public static void main(String[] args) { List bag = new ArrayList(); Producer p = new…
wj539h
  • 11
  • 3
0
votes
1 answer

C - Pthreads, one consumer, multiple producers synchronisation

I have a university assignment where I have to use threads to do some calculations. It boils down to one consumer with multiple producers --> each producer does one calculation, consumer adds it all together. I'm having trouble synchronising this so…
0
votes
1 answer

Multithreaded producer/consumer with four queues

I separated the consumer/producer problem from my application to be sure my threads work as they should. I have one producer thread and a thread pool of consumers: in my application, one thread accepts connections and queue them up (within a custom…
elmazzun
  • 1,066
  • 2
  • 18
  • 44
0
votes
1 answer

Strange deadlock in producer-consumer queue

With C's pthread library, I'm trying to implement a simple producer-consumer schema. The producer generates random numbers and puts them into a queue like this typedef struct { int q[MAX_QUEUE]; int head; int tail; } queue; The consumer…
Davide R.
  • 207
  • 2
  • 8
0
votes
1 answer

Reading lots of files using producer causes CPU usage to be 100%

I wrote a simple consumer-producer pattern to help me achieve the following task: Read a file from a directory that contain ~500,000 TSV (tab-separated) files. Manipulate each file into a data structure and put it in blocking queue. Consume the…
ocp1000
  • 571
  • 1
  • 5
  • 12
0
votes
4 answers

possible memory leak in a singleton?

I've asked this question before with no real answer. Can anybody help? I'm profiling the below code inside a singleton and found that a lot of Rate objects (List) are kept in memory although I clear them. protected void FetchingRates() { int…
user437631
  • 1,112
  • 4
  • 12
  • 28
0
votes
1 answer

Slow pthread consumer

I've implemented a solution the producer / consumer problem in C using pthreads and semaphores. My main thread is the producer and I launch N consumer threads. My code is: typedef struct { int buf[BUFSIZE]; /* shared var */ int in; …
rafaelcpalmeida
  • 874
  • 1
  • 9
  • 28
0
votes
1 answer

How do I group consumers in RabbitMQ?

We are writing mail sync system, and we use RabbitMQ for that. Every producer pushes mails ids, then consumer gets ids and insert mails to db. In situation when we have 100 consumers (for example) and producers will generate ids too fast, every…
0
votes
0 answers

Synchronizing queue

I am trying to implement queue to synchronize producer-consumer problem. However I cannot figure out why my solution deadlocks. What it should do: Queue with fixed size containing tasks for consumer threads, producers should be blocked until there…