QThreadPool reserveThread 例子
QThreadPool reserveThread example
任何人都可以提供使用 QThreadPool class 中的 "reserveThread" and/or "releaseThread" 的示例吗?我已经阅读了文档,但我不太了解您何时会使用这些功能。示例的互联网搜索结果为空。
我正在使用 PySide,所以 Python 是首选,但 C++ 也不错。
这些方法用于将线程池与您手动管理的线程进行互操作。
线程池保持活动线程的计数,并旨在不超过给定硬件上有意义的最大线程数。 reserveThread
和 releaseThread
更改池知道的活动线程数。它不会直接从池中添加或删除任何线程。 这些方法不 return QThread
并不是错误。
reserveThread
表示:“我正在使用我在别处管理的线程,所以请认为我的线程处于活动状态,即使它不是您的(线程池的)。
releaseThread
表示:"I'm not using my thread anymore, feel free to make more of your threads active."
示例:考虑一个四逻辑 CPU 系统。代码是C++。
最初:
QThreadPool pool;
assert(pool.maxThreadCount() == 4);
assert(pool.activeThreadCount() == 0);
您启动一个专用计算线程:一个核心变得繁忙。您通过调用 reserveThread
:
通知矿池
MyWorker worker;
QThread thread;
worker.moveToThread(&thread);
thread.start();
pool.reserveThread();
assert(pool.activeThreadCount() == 1);
池本身没有 运行任何线程!
您提交了四个 运行nables,每个都需要一段时间。该池创建三个额外的线程来执行它们:
QAtomicInt act = 0;
QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref(); });
QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref(); });
QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref(); });
QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref(); });
QThread::sleep(1);
assert(pool.activeThreadCount() == 4);
assert(act.load() == 3);
只有三个 运行nables 现在处于活动状态,因为四分之一的线程被保留并且不能处于活动状态:它没有 CPU 到 运行,因为你的线程在那里很忙。
你的计算线程完成了,你释放了一个核心。您通过调用 releaseThread
:
通知矿池
thread.quit();
thread.wait();
pool.releaseThread();
QThread::sleep(1);
assert(pool.activeThreadCount() == 4);
assert(act.load() == 4);
由于有一个额外的 运行nable 等待,一个线程被激活以使 运行nable 继续。
一分钟后,所有的运行启用都完成了,没有更多的活动线程:
QThread::sleep(60);
assert(pool.activeThreadCount() == 0);
assert(act.load() == 0);
任何人都可以提供使用 QThreadPool class 中的 "reserveThread" and/or "releaseThread" 的示例吗?我已经阅读了文档,但我不太了解您何时会使用这些功能。示例的互联网搜索结果为空。
我正在使用 PySide,所以 Python 是首选,但 C++ 也不错。
这些方法用于将线程池与您手动管理的线程进行互操作。
线程池保持活动线程的计数,并旨在不超过给定硬件上有意义的最大线程数。 reserveThread
和 releaseThread
更改池知道的活动线程数。它不会直接从池中添加或删除任何线程。 这些方法不 return QThread
并不是错误。
reserveThread
表示:“我正在使用我在别处管理的线程,所以请认为我的线程处于活动状态,即使它不是您的(线程池的)。
releaseThread
表示:"I'm not using my thread anymore, feel free to make more of your threads active."
示例:考虑一个四逻辑 CPU 系统。代码是C++。
最初:
QThreadPool pool; assert(pool.maxThreadCount() == 4); assert(pool.activeThreadCount() == 0);
您启动一个专用计算线程:一个核心变得繁忙。您通过调用
通知矿池reserveThread
:MyWorker worker; QThread thread; worker.moveToThread(&thread); thread.start(); pool.reserveThread(); assert(pool.activeThreadCount() == 1);
池本身没有 运行任何线程!
您提交了四个 运行nables,每个都需要一段时间。该池创建三个额外的线程来执行它们:
QAtomicInt act = 0; QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref(); }); QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref(); }); QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref(); }); QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref(); }); QThread::sleep(1); assert(pool.activeThreadCount() == 4); assert(act.load() == 3);
只有三个 运行nables 现在处于活动状态,因为四分之一的线程被保留并且不能处于活动状态:它没有 CPU 到 运行,因为你的线程在那里很忙。
你的计算线程完成了,你释放了一个核心。您通过调用
通知矿池releaseThread
:thread.quit(); thread.wait(); pool.releaseThread(); QThread::sleep(1); assert(pool.activeThreadCount() == 4); assert(act.load() == 4);
由于有一个额外的 运行nable 等待,一个线程被激活以使 运行nable 继续。
一分钟后,所有的运行启用都完成了,没有更多的活动线程:
QThread::sleep(60); assert(pool.activeThreadCount() == 0); assert(act.load() == 0);