Search code examples
c++multithreadingc++11thread-safetyproducer-consumer

C++ - Producer / Consumer only allow consumption in defined chunks


There's two threads A (Producer) and B (Consumer).

The data A produces is only meant to be read in chunks, hence B shall only be allowed to read once A has produced a whole chunk. A single piece of data is a simple struct and the chunk length is variable. For example once it could be that B is allowed to read after 50 pieces of data are produced, another time it might be 200.

I've found this implementation of a producer/consumer queue that I'd like to use: https://github.com/cameron314/readerwriterqueue

My current idea is that A writes its data into a std::vector and then pass the std::vector into the queue. But I doubt this works since the queue does not know how much memory an std::vector will take and it wants to allocate the memory beforehand.

I hope anybody knows an easier solution for this.


Solution

  • Regardless of what you produce or consume, you will need a concurrent queue for communication between the producer(s) and the consumer(s). If we are doing it C++ style, you'll end up with something like:

    template<typename T, typename Alloc>
    class concurrent_queue;
    

    (note that some libraries already give you such containers, Intel TBB for example).

    The template parameter T is what you exchange between producers and consumers. As you asked to consume chunks, here T = your_chunk_type. Let's say your chunks are variable size: Chunk = std::vector<something>. With the lib you've linked on github, you could just use ReaderWriterQueue<Chunk> as the queue sharing the work.