QRunnable - how to use it, examples

拜拜、爱过 提交于 2019-12-23 09:58:20

问题


Could point at some code using QRunnable as an alternative to QtConcurrent: I can't find any QRunnable example in Qtdoc.

Did you ever try QRunnable AND QtConcurrent for same application, and could you comment on the compared performance?


回答1:


QRunnable is an interface. So rather than looking for "a QRunnable example" it would be better to look for (for instance) "a QThreadPool example", such as:

http://doc.qt.io/qt-4.8/thread-basics.html#example-1-using-the-thread-pool

If you read further on that page, it mentions the real value in QtConcurrent is when you are doing something similar to applying an STL algorithm to an STL container. Using a thread pool with QRunnable is better for when you just have a bunch of fairly unrelated tasks to perform.

QtConcurrent is built on top of QThreadPool. It's notationally convenient and keeps you from having to write the patterns yourself, but is not going to intrinsically speed you up over what you could hand code. But...thinking in terms of QtConcurrent patterns (such as MapReduce) may help you see opportunities for parallelism you wouldn't otherwise think of, and writing less code means it's easier to try alternative approaches and compare their performance.



来源:https://stackoverflow.com/questions/10127301/qrunnable-how-to-use-it-examples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!