C++: synchronize 5 consumers to 1 producer (multithreaded)

I have five consumers and one producer. The five consumers each output different data, from the one producer, for ~10ms. During those 10ms the producer prepares the parameters for the next output. When the output parameters are set, I want to set a flag instructing the consumers to begin the next output. I only want the producer to produce when the consumers are outputting the data.

I am unsure how to synchronize the five consumers and the single producer. I currently have two flags, runFlag and doneFlag. When a consumer reads the new data I want to set runFlag to true so the calculations begin, and I want to set doneFlag to false, as the calculations have not completed. However, if I set doneFlag to false in one consumer it may be false in another consumer before that consumer can check the flag.

I hope my question is specific enough. Please let me know if there's anything else I can provide. Also, I'm just looking for a general idea of how to proceed. I know there are multiple ways to do this, but I'm unsure which method will work the best.

Thanks!

-------------Problems Reply------------

You will need 2 events and an integer reference count.

When producer has produced some thing it:

  • initiates read_count = 0;
  • sets event readme.
  • starts to wait on event completed;

Consumers wait on event readme. After doing their work they ATOMICALLY increment read_count. If the read_count reaches the number of consumers, 5 in your case, then it sets the completed event. Thus producer can continue and the cycle repeats itself.

A few years back, I had to create a generic work dispatcher that does post-processing. Its not a producer-consumer exactly and may be overkill for your app, but it may give you some ideas.

I particularly like using a pair of in-memory shared queues, an outbound queue and an inbound queue, arranged like a two-way channel. If you create a queue class that has the proper synchronization for reading and writing, the producer and consumers can become independent. They don't need to know how to synchronize with each other.

Your data, known to the producer and consumer, referenced in a work item class. The work item class contains all of the status flags. The data should also be thread safe.

The producer enqueues work items onto the outbound queue and each consumer dequeues a single work item. When the work is completed, the status flags are updated and the work item is posted back to inbound queue for post-processing by the producer.

IIRC, the architecture only contain three classes or so.

Category:c# Views:0 Time:2010-06-04

Related post

  • Handling producer thread when no consumers are ready 2011-10-16

    Suggestions on patterns for handling the following scenario: A single thread that dispatches events to consumers. There is a 1:1 between each event and a consumer (each event is dispatched to a single consumer based on event/consumer id match). Consu

  • Multithreading Puzzles 2009-12-07

    I'm trying to come up with some programming puzzles focused on multi-threading. Most of the problems I've been able to come up with, so far, have been pretty domain specific. Does anybody have any decent programming puzzles for developers attempting

  • producer-consumer problem:posix mutex got locked twice when using condition variable? 2010-08-09

    Following code is only to show how to use condition variable to synchronize threads(one producer and many consumers) as exercising. See the line for code 'usleep(100);'.When I comment this line,two consumer threads seem got the mutex locked at the sa

  • Does .NET 4 Parallel Extensions include an implementation of a lock-free Producer/Consumer queue? 2009-12-06

    Does .NET 4 Parallel Extensions include an implementation of a lock-free Producer/Consumer queue? Is there any class or interface to implement which could help me to implement this pattern correctly? Thanks! --------------Solutions------------- I don

  • Producer consumer with qualifications 2010-05-03

    I am new to clojure and am trying to understand how to properly use its concurrency features, so any critique/suggestions is appreciated. So I am trying to write a small test program in clojure that works as follows: there 5 producers and 2 consumers

  • How to tell producer to not wait in producer-consumer pattern using C# TPL 2010-09-21

    I implements a producer-consumer pattern using C#'s TPL. The scenario is described as below. I have a task who produces jobs to a BlockingCollection. The collection has a capacity limit, say, 3 jobs maximally. I have two consumer tasks who try to get

  • How to solve the producer-consumer using semaphores? 2011-11-27

    I need to code a problem similar to producer-consumer, that must use semaphores. I tried a couple of solutions and none of them worked. First I tried a solution on Wikipedia and it didn't worked. My current code is something like that: Method run of

  • Detect if a process is already running and collaborate with it 2010-01-08

    I'm trying to create a program that starts a process pool of, say, 5 processes, performs some operation, and then quits, but leaves the 5 processes open. Later the user can run the program again, and instead of it starting new processes it uses the e

  • What are the "things to know" when diving into multi-threaded programming in C++ 2010-01-22

    I'm currently working on a wireless networking application in C++ and it's coming to a point where I'm going to want to multi-thread pieces of software under one process, rather than have them all in separate processes. Theoretically, I understand mu

  • Measuring broadcast message latency using system clock, good idea? 2011-02-14

    I want to measure broadcast message latency over our message broker on a 1GB LAN. Messages are transmitted in a pub sub fashion, one publisher, many consumers. The producer timestamps each message using the system clock (DateTime.Now in C#) and consu

  • SIGSTOP and SIGCONT equivalent in threads 2011-06-16

    Is there something equivalent to SIGSTOP and SICONT for threads? Am using pthreads. Thanks An edit: I am implementing a crude form of file access syncronization among threads. So if a file is already opened by a thread, and another thread wants to op

  • Which features make a class to be thread-safe? 2011-07-13

    In MSDN some .NET classes described like this: "This type is thread safe." or "Public static (Shared in Visual Basic) members of this type are thread safe. Instance members are not guaranteed to be thread-safe.". My question is which features make a

  • Best way to distribute tasks considering latency and efficiency 2011-11-09

    I'm looking for an algorithm to distribute some tasks. The problem is as follows: Say I have a central task producer and some client consumers. The producer generates tasks and consumers take tasks (for starters, one at a time), process them, and whe

  • Does python support multiprocessor/multicore programming? 2008-10-15

    What is the difference between multiprocessor programming and multicore programming? preferably show examples in python how to write a small program for multiprogramming & multicore programming --------------Solutions------------- There is no suc

  • Pthread conditional signal - not working as expected 2009-03-22

    I am working on a project and trying to use pthread_cond_wait() and pthread_cond_signal() to synchronize two threads. My code looks something like this: pthread_mutex_t lock_it = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t write_it = PTHREAD_COND_INITI

  • Are private methods in general a code smell? 2009-04-21

    In the sense that a code smell is an indicator of a potential need for refactoring are private methods a code smell? I was looking at some of my own code and it dawned on me that many of my public methods don't actually do anything other than consoli

  • Delphi - adjusting thread sleep time 2009-06-06

    There are several threads in my application that work in the background. They connect to database and execute some time consuming select queries. In most of cases these queries return only several records. From time to time, however, they may return

  • c++ open source project recommend 2009-06-14

    I have learned c++ about three years and I have not used c++ in an actual project. I only used it to write some small program and example, I have read many books about c++ and algorithm, "c++ primer", "effective c++" "exceptional c++" "c++ common kno

  • Coding Standards for pure C (not C++) 2009-08-11

    I come from a java background (from my CS classes) and a semester of C++. I am just finishing up a OpenCV project for my Co-Op that's in pure C, so I'm a bit late in asking this question. What are the design processes and coding standards for pure C?

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.095 (s). 11 q(s)