设计模式,Qt模型/视图和多线程 [英] Design Pattern, Qt Model/View and multiple threads

查看:280
本文介绍了设计模式,Qt模型/视图和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个显示市场数据的应用程序,并以其他形式使用它。我在地图上存储市场数据,例如
std :: map< tickerId,StockData> 。让我给出一个关于如何使用该映射的使用情况。

I am creating an application which displays the market data and uses it in some other forms too. I store market data in a map say std::map<tickerId, StockData>. Let me give one used case of how this map can be used.


  1. 网络在时间t发送封装股票数据的数据分组。 updatePrice(tickerId,latestPrice)

  2. 更新地图中的股票数据。现在,多个线程可以访问/更新数据。因此,必须锁定地图以进行线程安全操作。这是第一个问题,我需要锁定底层数据以进行更新吗?

  3. 新的股票数据有多种用途,比如IBM有一个价格更新,我需要更新我的投资组合中的IBM的价值。以及在屏幕上显示新数据。还可以有其他几个同时使用。 updatePosition(tickerId,price) updateStockScreen(tickerId,price)。另外,从位置更新分离Gui更新是重要的,因为GUI不是应用程序的主要力量。

  4. 我只是困扰如何实现这种类型的设计。我阅读关于模型/视图设计在QT中显示数据,但如果View线程从同一个地图读取,它必须被锁定。这导致设计慢/低效。每次视图从模型读取时,模型需要被锁定。

  5. 总而言之,我已经存储了很多不同的对象作为地图。对象将实时更新。我需要更新它们,然后在不同的位置使用它们。如果有人能给我一个关于如何实现这样的设计的一个小例子,这将是巨大的。

  1. network sends a data packet encapsulating the stock Data at time t. updatePrice(tickerId, latestPrice)
  2. update the stock data in the map. Now, multiple threads can access/update the data. So the map has to be locked for thread-safe operations. Here is the first question, do I need to lock the underlying data too for updates?
  3. There are multiple uses of the new stock data, say, there is a price update on IBM, then I need to update the value of IBM in my portfolio. As well as display the new data on a screen. And there can be several other simultaneous uses.updatePosition(tickerId, price) and updateStockScreen(tickerId, price). Also, separting Gui updates from position update is important as GUI is not the main strength of the application.
  4. I am just troubled about how to implement this type of design. I read about Model/View Design in QT to display data but if View thread reads from the same map, it has to be locked. This leads to an slow/inefficient design. Every time view reads from the model, the model needs to be locked. Is this preffered in real-time GUIs?
  5. To summarize, I have stored a lot of different objects as maps. And objects are updated in realtime. I need to update them and then use them at various locations. It would be great if someone can give me a small example on how to implement such designs.

一些有用的书籍也值得赞赏。

Some references to useful books are appreciated too.

我是新的,试图用我的小知识实现太多,所以请原谅我,如果我问了愚蠢/有问题的问题。

I am new and trying to achieve too much with my little knowledge so forgive me if I have asked stupid/ill-formed questions.

感谢
Shiv

Thanks Shiv

推荐答案

听起来像是你想要的模型在一个线程,

It sounds conceptually like you want the model on one thread and the view on another, which I looked into at one point.

如果是这样...你的模型是只读的通过视图小部件,然后是,你必须锁。我认为这样做会破坏模型/视图分离提供的解耦的优雅。但它可以工作。

If so...and your model is read-only through the view widget then yes, you have to lock. I'd argue that doing so undermines the elegance of the "decoupling" provided by the model/view separation. But it could be made to work.

但是...如果你的模型是读写通过视图,这是不可能做到正确在所有<强>因为通知槽的排队性质。这是一个邮件列表对话的档案在qt兴趣邮件列表上有关主题:

However...if your model is read-write through the view it's not possible to do correctly at all because of the queued nature of the notification slots. Here's an archive of a mailing list conversation I had on the qt-interest mailing list on the topic:

http://blog.hostelfork.com/qt-model-view-different-threads/


简短的版本是,我不认为这是可行的模型

在非GUI线程上修改...无论如何如果我收集的

是正确的,那么Qt应该有一个断言模型和

它的视图有相同的线程亲和力(现在似乎没有这样做)

"The short version is that I don't think it's feasible for a Model to
be modified on a non-GUI thread...regardless of whether the model's
data has been protected with read/write locks. If what I'm gathering
is correct, then Qt should probably have an assert that a model and
its view have the same thread affinity (it doesn't seem to do that now)"

测试由一个KDE开发人员验证这一点。

A subsequent unit test by a KDE developer verified this.

我觉得解决这个问题的最好的方法是保持模型和视图在同一个线程,只有修改模型在GUI线程中。因此,如果工作线程希望改变它,那么它应该使用一个信号。

I feel the best way to work around this is to keep the model and the view on the same thread, and only modify the model in the GUI thread. So if the worker thread wishes to change it then it should use a signal.

工作者是否需要保留自己创建模型的数据的副本或者如果需要获取通知以保持最新的用户更改模型通过视图)取决于您的应用程序。如果我正确理解你,它听起来像你可能会离开,只是通过信号/插槽和忘记他们的工人更新...

Whether the worker needs to keep their own copy of the data from which the model was created (or if it needs to get notifications to keep that up to date when the user changes the model through the view) depends on your app. If I understand you correctly, it sounds like like you could probably get away with just ferrying the updates through signal/slots and forgetting them on the worker...

这篇关于设计模式,Qt模型/视图和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆