一、QThreadPool类

  QThreadPool管理一组线程。它负责管理和回收单个QThread对象以减少程序中线程创建的开销。每个Qt应用程序都有一个全局的QThreadPool对象,可通过方法globalInstance()获得。为了调用QThreadPool中的一个线程,需要提供一个从QRunnable继承过来的类,并实现其中的run方法。然后创建一个该类的对象,传递给QThreadPool::start()方法。代码片断如下:

  1. class HelloWorldTask : public QRunnable
  2. {
  3. void run()
  4. {
  5. qDebug() << "Hello world from thread" << QThread::currentThread();
  6. }
  7. }
  8. HelloWorldTask *hello = new HelloWorldTask();
  9. // QThreadPool takes ownership and deletes 'hello' automatically
  10. QThreadPool::globalInstance()->start(hello);

默认情况下, QThreadPool自动删除QRunnable对象。使用QRunnable::setAutoDelete()方法可以改变该默认行为。QThreadPool支持在QRunnable::run方法中通过调用tryStart(this)来多次执行相同的QRunnable。当最后一个线程退出run函数后,如果autoDelete启用的话,将删除QRunnable对象。在autoDelete启用的情况下,调用start()方法多次执行同一QRunnable会产生竞态,就避免这样做。

  那些在一定时间内会使用的线程将会过期。默认的过期时间是30秒。可通过setExpiryTimeout()方法来设置。设置一个负数的超时值代表禁用超时机制。方法maxThreadCount()可以查询可使用的最大线程数,你也可以设置最大的线程数。activeThreadCount反应的是当前正在被使用中的线程数个数。reserveThread函数保留某个线程为外部使用,releaseThread释放该线程,这样就可以被再次使用。

二、QtConcurrent命名空间

QtConcurrent命名空间里提供了一些高级API,利用这些API可以编写多线程程序,而不用直接使用比较低级的一些类,如mutext,lock, waitcondition以及semaphore等。使用QtConcurrent命令空间的API编写的程序会根据处理器的数目自动地调整线程的个数。QtConcurrent包含了用于并行列表处理的函数式编程,包含实现共享内存系统的MapReduce和FilterReduce, 以及管理GUI应用程序中异步计算的类。相关的类说明如下:

QtConcurrent::map()

appliesa function to every item in a container, modifying the itemsin-place

QtConcurrent::mapped()

islike map(), except that it returns a new container with themodifications

QtConcurrent::mappedReduced()

islike mapped(), except that the modified results are reduced orfolded into a single result.

QtConcurrent::filter()

litems from a container based on the result of a filter function.

QtConcurrent::filtered()

islike filter(), except that it returns a new container with thefiltered results

QtConcurrent::filteredReduced()

islike filtered(), except that the filtered results are reduced orfolded into a single result

QtConcurrent::run()

runsa function in another thread.

QFutureIterator

allowsiterating through results available via QFuture.

QFutureWatcher

allowsmonitoring a QFuture usingsignals-and-slots.

QFutureSynchronizer

isa convenience class that automatically synchronizes severalQFutures.

代码实例:

    1. using namespace QtConcurrent;
    2. void hello(QString name)
    3. {
    4. qDebug() << "Hello" << name << "from" << QThread::currentThread();
    5. }
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app(argc, argv);
    9. QFuture<void> f1 = run(hello, QString("Alice"));
    10. QFuture<void> f2 = run(hello, QString("Bob"));
    11. f1.waitForFinished();
    12. f2.waitForFinished();
    13. return app.exec();
    14. }

QThreadPool类和QtConcurrent命名空间的更多相关文章

  1. .NET快速查找某个类所在的命名空间

    有时候我们从网上copy别人的代码下来,对于某些不熟悉的类,需要添加对某个类的引用时,如何快速找出某个类所在的命名空间呢 例如有如下的一段代码: 现在要添加ConfigurationElement类的 ...

  2. python学习之老男孩python全栈第九期_day023知识点总结——类和对象命名空间、组合

    一. 类和对象命名空间类里 可以定义两种属性: 1. 静态属性 2. 动态属性 class Course: language = 'Chinese' def __init__(self, teache ...

  3. Qt 线程池QThreadPool类、QRunnable类

    QThreadPool类 用来管理 QThreads.此类中的所有函数都是线程安全的. 主要属性: 1.activeThreadCount: 此属性表示线程池中的活动线程数,通过activeThrea ...

  4. Unity 改变类模板-为你的类添加一个命名空间

    之前在写代码的时候,就很疑惑为什么创建类的时候.没有命名空间呢?   后来自己的类终于和别人写的类名字有冲突.... 如何修改Unity创建类的模板呢?  找到下面这个文件 然后修改 保存文件在Uni ...

  5. python--面向对象:类和对象命名空间

    一.一个类可以定义两种属性:静态属性和动态属性 (一)对于不可变数据类型来说,类变量最好用类名操作,也可以用对象操作,但是只能查,不能改,对象改的都只是相当于在自己的命名空间里重新建立了一个 clas ...

  6. C# 使用Nlog记录日志到数据库 使用LogEventInfo类获取,命名空间名称、类名、方法名

    原文地址:http://dotnet.9sssd.com/csbase/art/793 [摘要]Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台,保存到文本,也可以很方便的记录到数 ...

  7. python:类与对象命名空间、面对对象的组合用法

    1,类里可以定义两种属性: #静态属性 #静态属性就是直接在类中定义的变量 #动态属性 #动态属性就是定义在类中的方法 class Course: language = ['Chinese']#静态属 ...

  8. Qt新建线程的方法(有QRunnable,QThreadPool,moveToThread和QtConcurrent的例子)

    看了不少Qt线程的东西,下面总结一下Qt新建一个线程的方法. 一.继承QThread 继承QThread,这应该是最常用的方法了.我们可以通过重写虚函数void QThread::run ()实现我们 ...

  9. Qt 线程基础(QThread、QtConcurrent、QThreadPool等)

      使用线程 基本上有种使用线程的场合: 通过利用处理器的多个核使处理速度更快. 为保持GUI线程或其他高实时性线程的响应,将耗时的操作或阻塞的调用移到其他线程. 何时使用其他技术替代线程 开发人员使 ...

随机推荐

  1. JS中this和call

    首先来了解一下JS中this的原理: 要访问自己的属性就必须使用 this.属性名 1.this总是指向它的直接调用者: var a={ user:'Artimis', fn:function(){ ...

  2. vue获取当前路由

    完整url可以用 window.location.href路由路径可以用 this.$route.path路由路径参数 this.$route.params 例如:/user/:id → /user/ ...

  3. 关于cubic-bezier 贝塞尔曲线的简单了解

    在animation和transition两个属性中,cubic-bezier是控制变化的速度曲线,主要是生成速度曲线的函数 规定用法是: cubic-bezier(<x1>,<y1 ...

  4. KVM之virsh管理虚拟机网卡配置

    虚拟机网卡管理 virsh attach-interface 添加网卡: [root@ubuntu ~]# virsh domiflist CentOS-V6.5.23-server01 Interf ...

  5. vuejs开发流程

    https://www.cnblogs.com/yexiaowang/p/8489250.html

  6. Rocketmq 集群部署

    10.1.0.178 配置文件 broker-a-m.properties brokerClusterName=PaymentClusterbrokerName=broker-anamesrvAddr ...

  7. ubuntu---解决pip安装tf很慢的问题

    ubuntu---解决pip安装tf很慢的问题 [问题] 执行 u@u160406:~$ sudo pip3 install tensorflow-gpu==1.15.0rc2 下载速度真的很慢 Lo ...

  8. Web UI开发速速种草—Kendo UI for jQuery网格编辑操作概述

    Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...

  9. 月球-I型,月份日历生成器----基于PHP7.3

    生成月份周日的类 <?php class mycalendar { function __construct($year,$mon) { $'; $this->firstday=strto ...

  10. 13、Spring Boot 2.x 多数据源配置

    1.13 Spring Boot 2.x 多数据源配置 完整源码: Spring-Boot-Demos