producer-consumer

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

家住魔仙堡 提交于 2019-12-22 10:23:08
问题 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

Fast C++ single producer single consumer implementation

我只是一个虾纸丫 提交于 2019-12-22 08:53:46
问题 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

C++11 non-blocking producer/consumer

筅森魡賤 提交于 2019-12-22 04:31:34
问题 I have a C++11 application with a high-priority thread that's producing data, and a low-priority thread that's consuming it (in my case, writing it to disk). I'd like to make sure the high-priority producer thread is never blocked, i.e. it uses only lock-free algorithms. With a lock-free queue, I can push data to the queue from the producer thread, and poll it from the consumer thread, thus meeting my goals above. I'd like to modify my program so that the consumer thread blocks when inactive

Efficient consumer thread with multiple producers

旧城冷巷雨未停 提交于 2019-12-22 04:16:23
问题 I am trying to make a producer/consumer thread situation more efficient by skipping expensive event operations if necessary with something like: //cas(variable, compare, set) is atomic compare and swap //queue is already lock free running = false // dd item to queue – producer thread(s) if(cas(running, false, true)) { // We effectively obtained a lock on signalling the event add_to_queue() signal_event() } else { // Most of the time if things are busy we should not be signalling the event add

Why GC does not collect unused objects?

浪尽此生 提交于 2019-12-22 01:40:13
问题 I implemented Producer/Consumer pattern with BlockingCollection for my experiment. PerformanceCounter c = null; void Main() { var p =System.Diagnostics.Process.GetCurrentProcess(); c = new PerformanceCounter("Process", "Working Set - Private", p.ProcessName); (c.RawValue/1024).Dump("start"); var blocking = new BlockingCollection<Hede>(); var t = Task.Factory.StartNew(()=>{ for (int i = 0; i < 10000; i++) { blocking.Add(new Hede{ Field = string.Join("",Enumerable.Range(0,100).Select (e => Path

How to limit BlockingCollection size but keep adding new itens (.NET limited size FIFO)?

微笑、不失礼 提交于 2019-12-21 22:39:43
问题 I want to limit the size of the BlockingCollection. If I want to add another item and the collection is full, the oldest must be removed. Is there some Class specific to this task or my solution is ok? BlockingCollection<string> collection = new BlockingCollection<string>(10); string newString = ""; //Not an elegant solution? if (collection.Count == collection.BoundedCapacity) { string dummy; collection.TryTake(out dummy); } collection.Add(newString); EDIT1: Similar question here: ThreadSafe

producer - consumer multithreading in Java

霸气de小男生 提交于 2019-12-21 17:48:15
问题 I want to write program using multithreading wait and notify methods in Java. This program has a stack (max-length = 5). Producer generate number forever and put it in the stack, and consumer pick it from stack. When stack is full producer must wait and when stack is empty consumers must wait. The problem is that it runs just once, I mean once it produce 5 number it stops but i put run methods in while(true) block to run nonstop able but it doesn't. Here is what i tried so far. Producer class

Producer/consumer pattern with a fixed-size FIFO queue

陌路散爱 提交于 2019-12-21 04:53:45
问题 I need to implement the producer/consumer pattern around a fixed-size FIFO queue. I think a wrapper class around a ConcurrentQueue might work for this but I'm not completely sure (and I've never worked with a ConcurrentQueue before). The twist in this is that the queue needs to only hold a fixed number of items (strings, in my case). My application will have one producer task/thread and one consumer task/thread. When my consumer task runs, it needs to dequeue all of the items that exist in

How to make worker threads quit after work is finished in a multithreaded producer-consumer pattern?

£可爱£侵袭症+ 提交于 2019-12-20 12:34:45
问题 I am trying to implement a multithreaded producer-consumer pattern using Queue.Queue in Python 2.7. I am trying to figure out how to make the consumers, i.e. the worker threads, stop once all required work is done. See the second comment by Martin James to this answer: https://stackoverflow.com/a/19369877/1175080 Send an 'I am finished' task, instructing the pool threads to terminate. Any thread that gets such a task requeues it and then commits suicide. But this does not work for me. See the

is it good to use BlockingCollection<T> as single-producer, single-consumer FIFO query?

元气小坏坏 提交于 2019-12-19 02:46:14
问题 I need single-producer, single-consumer FIFO query because I need to process messages in the order they received. I need to do this asynchronous because caller should not wait while I'm processing message. Next message processing should be started only when previous message processing is finished. Sometimes frequency of "receiving" messages is higher than frequency of "processing" messages. But in average I should be able to process all messages, just sometimes I have to "queue" pack of them.