问题
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