<legend id='47OnV'><style id='47OnV'><dir id='47OnV'><q id='47OnV'></q></dir></style></legend>
<tfoot id='47OnV'></tfoot>
    • <bdo id='47OnV'></bdo><ul id='47OnV'></ul>
    1. <small id='47OnV'></small><noframes id='47OnV'>

      1. <i id='47OnV'><tr id='47OnV'><dt id='47OnV'><q id='47OnV'><span id='47OnV'><b id='47OnV'><form id='47OnV'><ins id='47OnV'></ins><ul id='47OnV'></ul><sub id='47OnV'></sub></form><legend id='47OnV'></legend><bdo id='47OnV'><pre id='47OnV'><center id='47OnV'></center></pre></bdo></b><th id='47OnV'></th></span></q></dt></tr></i><div id='47OnV'><tfoot id='47OnV'></tfoot><dl id='47OnV'><fieldset id='47OnV'></fieldset></dl></div>

        设计模式、Qt 模型/视图和多线程

        Design Pattern, Qt Model/View and multiple threads(设计模式、Qt 模型/视图和多线程)

      2. <tfoot id='CdP1G'></tfoot>

        <small id='CdP1G'></small><noframes id='CdP1G'>

        <legend id='CdP1G'><style id='CdP1G'><dir id='CdP1G'><q id='CdP1G'></q></dir></style></legend>

              <i id='CdP1G'><tr id='CdP1G'><dt id='CdP1G'><q id='CdP1G'><span id='CdP1G'><b id='CdP1G'><form id='CdP1G'><ins id='CdP1G'></ins><ul id='CdP1G'></ul><sub id='CdP1G'></sub></form><legend id='CdP1G'></legend><bdo id='CdP1G'><pre id='CdP1G'><center id='CdP1G'></center></pre></bdo></b><th id='CdP1G'></th></span></q></dt></tr></i><div id='CdP1G'><tfoot id='CdP1G'></tfoot><dl id='CdP1G'><fieldset id='CdP1G'></fieldset></dl></div>
                  <tbody id='CdP1G'></tbody>
                  <bdo id='CdP1G'></bdo><ul id='CdP1G'></ul>
                • 本文介绍了设计模式、Qt 模型/视图和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在创建一个显示市场数据并以其他形式使用它的应用程序.我将市场数据存储在地图中说std::map.让我举一个如何使用这张地图的用例.

                  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 中阅读了模型/视图设计以显示数据,但如果视图线程从同一地图读取,则必须将其锁定.这会导致设计缓慢/低效.每次视图从模型中读取时,都需要锁定模型.这在实时 GUI 中是首选吗?
                  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.

                  谢谢湿婆

                  推荐答案

                  从概念上讲,您希望模型在一个线程上,而视图在另一个线程上,我曾研究过这一点.

                  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-interest 邮件列表上关于该主题的邮件列表对话的存档:

                  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.hostilefork.com/qt-model-view-不同的线程/

                  简短的版本是我认为模型不可行
                  在非 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 模型/视图和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                      <small id='eFeyf'></small><noframes id='eFeyf'>

                      <tfoot id='eFeyf'></tfoot>
                          <bdo id='eFeyf'></bdo><ul id='eFeyf'></ul>

                          <legend id='eFeyf'><style id='eFeyf'><dir id='eFeyf'><q id='eFeyf'></q></dir></style></legend>
                            <tbody id='eFeyf'></tbody>
                          <i id='eFeyf'><tr id='eFeyf'><dt id='eFeyf'><q id='eFeyf'><span id='eFeyf'><b id='eFeyf'><form id='eFeyf'><ins id='eFeyf'></ins><ul id='eFeyf'></ul><sub id='eFeyf'></sub></form><legend id='eFeyf'></legend><bdo id='eFeyf'><pre id='eFeyf'><center id='eFeyf'></center></pre></bdo></b><th id='eFeyf'></th></span></q></dt></tr></i><div id='eFeyf'><tfoot id='eFeyf'></tfoot><dl id='eFeyf'><fieldset id='eFeyf'></fieldset></dl></div>