简体   繁体   English

多个生产者单个消费者队列

[英]Multiple Producers Single Consumer Queue

I am new to multithreading and have designed a program that receives data from two microcontroller measuring various temperatures (Ambient and Water) and draws the data to the screen. 我是多线程的新手,他设计了一个程序,从两个微控制器接收数据,测量各种温度(环境和水),并将数据绘制到屏幕上。 Right now the program is singly threaded and its performance SUCKS A BIG ONE. 现在该程序是单线程的,它的性能很快。

I get basic design approaches with multithreading but not well enough to create a thread to do a task but what I don't get is how to get threads to perform seperate task and place the data into a shared data pool. 我得到了多线程的基本设计方法,但不足以创建一个执行任务的线程,但我没有得到的是如何让线程执行单独的任务并将数据放入共享数据池。 I figured that I need to make a queue that has one consumer and multiple producers (would like to use std::queue). 我想我需要创建一个具有一个使用者和多个生成器的队列(想要使用std :: queue)。 I have seen some code on the gtkmm threading docs that show a single Con/Pro queue and they would lock the queue object produce data and signal the sleeping thread that it is finished then the producer would sleep. 我在gtkmm线程文档中看到了一些显示单个Con / Pro队列的代码,他们会锁定队列对象产生数据并向睡眠线程发出信号表明它已完成然后生产者将进入休眠状态。 For what I need would I need to sleep a thread, would there be data conflicts if i didn't sleep any of the threads, and would sleeping a thread cause a data signifcant data delay (I need realtime data to be drawn 30 frames a sec) 对于我需要的东西,我需要睡一个线程,如果我没有睡眠任何线程会有数据冲突,并且会睡眠线程导致数据显着的数据延迟(我需要实时数据绘制30帧a秒)

How would I go about coding such a queue using the gtkmm/glibmm library. 我将如何使用gtkmm / glibmm库编写这样的队列。

If you're looking for a lock free implementation of this, you won't find one. 如果你正在寻找一个无锁的实现,你将找不到。 When data structures are being written to, something needs to keep two threads from simultaneously updating the data structure and corrupting it. 在写入数据结构时,某些东西需要保持两个线程同时更新数据结构并破坏它。

Is there any reason you can't have each thread collect on it's own, with it's own structure, and then combine the results at the end? 有没有什么理由你不能让它自己收集每个线程,它有自己的结构,然后在最后结合结果?

Here's a suggestion: 这是一个建议:
1. Have two threads, that are responsible for obtaining data and placing into a buffer. 1.有两个线程,负责获取数据并放入缓冲区。 Each thread has it's own (circular) buffer. 每个线程都有自己的(循环)缓冲区。
2. There will be a third thread that is responsible for getting data from the buffers and displaying on the screen. 2.将有第三个线程负责从缓冲区获取数据并在屏幕上显示。
3. The screen thread sends messages to the data threads requesting some data, then displays the data. 3.屏幕线程向请求某些数据的数据线程发送消息,然后显示数据。 The messages help synchronize execution and avoid dead-locks. 这些消息有助于同步执行并避免死锁。
4. None of the threads should "wait on single or multiple objects", but poll for events. 4.所有线程都不应“等待单个或多个对象”,而是轮询事件。

Think of this scenario using people. 想想使用人物的这种情况。 One person is delivering water temperature readings. 一个人正在提供水温读数。 Another person delivering ambient temperature readings. 另一个人提供环境温度读数。 A third person receives or asks for the data and displays the data (on a white board). 第三个人接收或询问数据并显示数据(在白板上)。 The objective is to keep everybody operating at maximum efficiency without any collisions. 目标是让每个人在没有任何碰撞的情况下以最高效率运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM