producer-consumer

producer - consume; how does the consumer stop?

倖福魔咒の 提交于 2019-12-06 04:02:53
问题 So I have simulated my producer consumer problem and I have the code below. My question is this: how does the consumer stops if he's in constant while(true). In the code below, I've added if (queue.peek()==null) Thread.currentThread().interrupt(); which works nicely in this example. But in my real world design, this doesn't work (sometimes it takes longer time to the producer to 'put' the data so the exception thrown in the consumer is incorrect. In general, I know I can put a 'poison' data

deadlock because of blocking Queue.get() method

China☆狼群 提交于 2019-12-06 03:32:50
As the title implies I have a deadlock and no idea why. I have multiple producers and only one consumer. The schedule_task method will get called by multiple processes after the thread has called the get method of the queue from logging import getLogger from time import sleep from threading import Event, Thread from multiprocessing import Process from Queue import Queue class TaskExecutor(object): def __init__(self): print("init taskExecutor") self.event = Event() self.taskInfos = Queue() task_thread = Thread(target=self._run_worker_thread) self._instantEnd = False self._running = True task

Producer consumer implementation in a block device driver?

做~自己de王妃 提交于 2019-12-06 03:19:19
I'm trying to implement a producer-consumer like situation in my block level driver (on linux kernel version 2.6.39.1). My block driver's make_request_fn receives a stream of struct bio from a userlevel application. On receiving these BIOs, they are queued up. Next, I create a new struct bio which will hold all the information present in the queued BIOs. This new "merged_bio" will be submitted only if there is a struct request slot available in the lower level driver's request_queue . In the meantime, my block driver will continue to receive BIOs from the userlevel application and will queue

How to implement blocking read using POSIX threads

别等时光非礼了梦想. 提交于 2019-12-06 03:08:34
问题 I would like to implement a producer/consumer scenario that obeys interfaces that are roughly: class Consumer { private: vector<char> read(size_t n) { // If the internal buffer has `n` elements, then dequeue them // Otherwise wait for more data and try again } public: void run() { read(10); read(4839); // etc } void feed(const vector<char> &more) { // Safely queue the data // Notify `read` that there is now more data } }; In this case, feed and run will run on separate threads and read should

Designing a component both producer and consumer in Kafka

丶灬走出姿态 提交于 2019-12-06 01:32:47
I am using Kafka and Zookeeper as the main components of my data pipeline, which is processing thousands of requests each second. I am using Samza as the real time data processing tool for small transformations that I need to make on the data. My problem is that one of my consumers (lets say ConsumerA ) consumes several topics from Kafka and processes them. Basically creating a summary of the topics that are digested. I further want to push this data to Kafka as a separate topic but that forms a loop on Kafka and my component. This is what bothers me, is this a desired architecture in Kafka?

C++ Templated Producer-Consumer BlockingQueue, unbounded buffer: How do I end elegantly?

烂漫一生 提交于 2019-12-05 19:46:59
I wrote a BlockingQueue in order to communicate two threads. You could say it follows the Producer-Consumer pattern, with an unbounded buffer. Therefore, I implemented it using a Critical Section and a Semaphore, like this: #pragma once #include "Semaphore.h" #include "Guard.h" #include <queue> namespace DRA{ namespace CommonCpp{ template<class Element> class BlockingQueue{ CCriticalSection m_csQueue; CSemaphore m_semElementCount; std::queue<Element> m_Queue; //Forbid copy and assignment BlockingQueue( const BlockingQueue& ); BlockingQueue& operator=( const BlockingQueue& ); public:

Fast C++ single producer single consumer implementation

纵然是瞬间 提交于 2019-12-05 17:46:17
I'm looking for a single-producer, single-consumer FIFO implementation that would perform faster than the normal lock-write-unlock-signal / waitForSignal-lock-read-unlock stuff. I'm looking for something supported by most POSIX operating systems (x86 specific is fine) written in either C or C++. I'm not looking to pass anything larger than a pointer. I'm not necessarily attached to the lock-free idea, but I do want something fast and correct. One of the papers I read on the subject mentioned a two-queue approach that seemed interesting, but I haven't been able to find much about that since

Java: Thread producer consumer what is the most efficient way to wait for data to be produced

陌路散爱 提交于 2019-12-05 06:37:45
When using BlockingQueue to consume data that is produced what is the most efficient method for waiting for the data to appear? Scenario: Step 1) The data list will be a data store where timestamps are added to. These timestamps are required to be ordered by closest to current time priority. This list may be empty. A thread will be inserting the timestamps into it. Produce Step 2) I want to consume the data in here in another thread that will take the timestamps from data and check if they are after the current time. Consumer then Produce Step 3) If they are after the current time then send

Suspend consumer in producer/consumer pattern

≯℡__Kan透↙ 提交于 2019-12-05 05:56:15
I have producer and consumer connected with BlockingQueue . Consumer wait records from queue and process it: Record r = mQueue.take(); process(r); I need pause this process for a while from other thread. How to implement it? Now I think implement it such, but it's looks like bad solution: private Object mLock = new Object(); private boolean mLocked = false; public void lock() { mLocked = true; } public void unlock() { mLocked = false; mLock.notify(); } public void run() { .... Record r = mQueue.take(); if (mLocked) { mLock.wait(); } process(r); } I think your solution is simple and elegant,

C Confused on how to initialize and implement a pthread mutex and condition variable

。_饼干妹妹 提交于 2019-12-05 04:51:29
问题 I'm a little bit confused on how to initialize and implement a pthread mutex and condition variable. The goal of the program is to have producers place a set number of ints in a queue and consumers take the ints out of the queue. I must also be able to define the number of producer and consumer threads that are created. In the starter code, I am give these: // Locks & Condition Variables pthread_mutex_t lock; // Lock shared resources among theads pthread_cond_t full; // Condition indicating