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

发布于 2024-08-30 19:37:10 字数 393 浏览 3 评论 0原文

我是多线程新手,设计了一个程序,该程序从两个测量各种温度(环境温度和水)的微控制器接收数据并将数据绘制到屏幕上。现在该程序是单线程的,其性能非常糟糕。

我获得了多线程的基本设计方法,但还不足以创建线程来执行任务,但我不知道如何让线程执行单独的任务并将数据放入共享数据池中。我认为我需要创建一个具有一个消费者和多个生产者的队列(想使用 std::queue)。我在 gtkmm 线程文档上看到了一些代码,这些代码显示了单个 Con/Pro 队列,它们会锁定队列对象生成数据并向休眠线程发出信号,表明它已完成,然后生产者将休眠。对于我所需要的,我是否需要休眠一个线程,如果我没有休眠任何线程,是否会出现数据冲突,并且休眠线程会导致数据显着的数据延迟(我需要绘制实时数据 30 帧)秒)

我将如何使用 gtkmm/glibmm 库编写这样的队列。

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). 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. 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)

How would I go about coding such a queue using the gtkmm/glibmm library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-09-06 19:37:10

这里有一个建议:
1. 有两个线程,负责获取数据并放入缓冲区。每个线程都有自己的(循环)缓冲区。
2. 第三个线程负责从缓冲区获取数据并显示在屏幕上。
3. 屏幕线程向数据线程发送消息请求一些数据,然后显示数据。这些消息有助于同步执行并避免死锁。
4. 任何线程都不应“等待单个或多个对象”,而应轮询事件。

用人来想象这个场景。一个人正在提供水温读数。另一个人提供环境温度读数。第三人接收或请求数据并显示数据(在白板上)。目标是让每个人都以最高效率运行,而不会发生任何碰撞。

Here's a suggestion:
1. Have two threads, that are responsible for obtaining data and placing into a buffer. 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.
3. The screen thread sends messages to the data threads requesting some data, then displays the data. 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.

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.

ζ澈沫 2024-09-06 19:37:10

如果您正在寻找一种无锁实现,那么您将找不到。当写入数据结构时,需要采取某种措施来防止两个线程同时更新数据结构并损坏它。

是否有任何原因不能让每个线程以其自己的结构自行收集,然后最后合并结果?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文