QRunnable-如何使用它,示例 [英] QRunnable - how to use it, examples

查看:263
本文介绍了QRunnable-如何使用它,示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用QRunnable替代QtConcurrent指向一些代码:我在Qtdoc中找不到任何QRunnable示例.

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

您是否曾经针对相同的应用程序尝试过QRunnable和QtConcurrent,您能否评论比较的性能?

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

推荐答案

QRunnable是一个接口.因此,与其寻找"QRunnable示例",不如寻找例如QThreadPool示例",例如:

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-使用线程池

如果您在该页面上进一步阅读,它会提到QtConcurrent中的实际值,即执行与将STL算法应用于STL容器类似的操作时的值.当您只需要执行许多完全无关的任务时,将QRpool与nunable线程池结合使用会更好.

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建立在QThreadPool之上.从概念上讲,它很方便,使您不必自己编写模式,但本质上不会加快您处理代码的速度.但是...从QtConcurrent模式(例如MapReduce)的角度考虑可能会帮助您看到您原本不会想到的并行性机会,而编写更少的代码意味着尝试替代方法并比较其性能会更容易.

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.

这篇关于QRunnable-如何使用它,示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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