一、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. }

最新文章

  1. [LeetCode] Number of Digit One 数字1的个数
  2. FingerprintJS - 在浏览器端实现指纹识别
  3. STL vector按多字段值排序
  4. 夺命雷公狗---node.js---16之项目的构建在node+express+mongo的博客项目1
  5. STM32串口USART1的使用方法
  6. hibernate级联与反向
  7. poj2528(线段树+离散化)Mayor&#39;s posters
  8. 稀疏表示(sparse representation)和字典学习
  9. ORACLE如何停止一个JOB
  10. wxWidgets显示视频
  11. 干货分享:MySQL之化险为夷的【钻石】抢购风暴【转载】
  12. 音频PCM编码
  13. 1.8 range
  14. 【python学习笔记】6.抽象
  15. 一个貌似比较吊的递归转换为loop--总算成功了.
  16. JAVA内部类_1
  17. 基于FT5x06嵌入式Linux电容触摸屏驱动
  18. 基于Springboot集成security、oauth2实现认证鉴权、资源管理
  19. Visual Studio(VS)秘钥集合
  20. Linux tail 命令

热门文章

  1. mac下自己实现re-sign.jar对apk进行重签名
  2. exp/imp 数据库数据导出/导入
  3. Java学习第二天之Java程序的基本规则
  4. IT基础架构
  5. jmeter——参数化、关联、断言
  6. spring实例化三:CglibSubclassingInstantiationStrategy
  7. Django内置email发送邮件
  8. JSONP劫持
  9. JS BOM基础 全局对象 window location history screen navigator
  10. pyecharts v1 版本 学习笔记 柱状图