問題描述
我正在創建一個顯示市場數據并以其他形式使用它的應用程序.我將市場數據存儲在地圖中說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.
- 網絡在時間 t 發送一個封裝股票數據的數據包.
updatePrice(tickerId, latestPrice)
- 更新地圖中的股票數據.現在,多個線程可以訪問/更新數據.因此必須鎖定映射以進行線程安全操作.這是第一個問題,我是否也需要鎖定底層數據以進行更新?
- 新股票數據有多種用途,比如 IBM 的價格更新,然后我需要更新我投資組合中 IBM 的價值.以及在屏幕上顯示新數據.并且可以同時使用多個其他用途.
updatePosition(tickerId, price)
和updateStockScreen(tickerId, price)
.此外,將 GUI 更新與位置更新分開也很重要,因為 GUI 不是應用程序的主要優勢. - 我只是對如何實現這種類型的設計感到困擾.我在 QT 中閱讀了模型/視圖設計以顯示數據,但如果視圖線程從同一地圖讀取,則必須將其鎖定.這會導致設計緩慢/低效.每次視圖從模型中讀取時,都需要鎖定模型.這在實時 GUI 中是首選嗎?
- 總而言之,我將許多不同的對象存儲為地圖.并且對象是實時更新的.我需要更新它們,然后在不同的位置使用它們.如果有人能給我一個關于如何實現此類設計的小例子,那就太好了.
- network sends a data packet encapsulating the stock Data at time t.
updatePrice(tickerId, latestPrice)
- 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?
- 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)
andupdateStockScreen(tickerId, price)
. Also, separting Gui updates from position update is important as GUI is not the main strength of the application. - 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?
- 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 模型/視圖和多線程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!