C++:将 5 个消费者同步到 1 个生产者(多线程)
我有五个消费者和一个生产者。五个消费者分别从一个生产者输出不同的数据,持续约 10 毫秒。在这 10 毫秒内,生产者为下一个输出准备参数。设置输出参数后,我想设置一个标志,指示消费者开始下一个输出。我只希望生产者在消费者输出数据时进行生产。
我不确定如何同步五个消费者和单个生产者。我目前有两个标志:runFlag 和doneFlag。当消费者读取新数据时,我想将 runFlag 设置为 true,以便计算开始,并且我想将 didFlag 设置为 false,因为计算尚未完成。但是,如果我在一个消费者中将 didFlag 设置为 false,则在该消费者可以检查该标志之前,另一个消费者中的 didFlag 可能为 false。
我希望我的问题足够具体。如果我还可以提供其他信息,请告诉我。另外,我只是在寻找如何进行的一般想法。我知道有多种方法可以做到这一点,但我不确定哪种方法最有效。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要 2 个事件和一个整数引用计数。
当生产者生产了一些东西时,它:
自述文件
。消费者等待事件
readme
。完成工作后,他们会自动增加read_count
。如果 read_count 达到消费者数量(在您的情况下为 5),则会设置completed
事件。因此生产者可以继续并且循环重复。You will need 2 events and an integer reference count.
When producer has produced some thing it:
readme
.Consumers wait on event
readme
. After doing their work they ATOMICALLY incrementread_count
. If the read_count reaches the number of consumers, 5 in your case, then it sets thecompleted
event. Thus producer can continue and the cycle repeats itself.几年前,我必须创建一个进行后处理的通用工作调度程序。确切地说,它不是生产者-消费者,并且可能对您的应用程序来说有点过大,但它可能会给您一些想法。
我特别喜欢使用一对内存共享队列,一个出站队列和一个入站队列,像双向通道一样排列。如果您创建一个具有适当的读写同步性的队列类,那么生产者和消费者就可以变得独立。他们不需要知道如何彼此同步。
您的数据为生产者和消费者所知,并在工作项类中引用。工作项类包含所有状态标志。数据也应该是线程安全的。
生产者将工作项排入出站队列,每个消费者将单个工作项出队。当工作完成时,状态标志将被更新,并且工作项将被发送回入站队列以供生产者进行后处理。
IIRC,该架构仅包含三个类左右。
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.