Qthread,为qthread添加功能
Qthread,adding function to qthread
我有这样的类
class Class1 : public QObject
{
Q_OBJECT
void a();
void b();
...........
void Class1:a()
{
for(int i=0;i<10;i++)
b();//I want here to make parallel
//and wait here all threads are done
}
我怎么能在这里使用 qhthread,我已经看过教程,它们都只是为了类而不是为了一个函数?
如果需要在单独的线程上运行函数,可以使用如下QtConcurrent
:
QtConcurrent::run(this, &Class1::b);
编辑:您可以使用QFutureSynchronizer
等待所有线程,不要忘记使用QThreadPool::globalInstance()->setMaxThreadCount()
分配所有线程。
编辑 2:您可以使用 synchronizer.futures()
来访问所有线程返回值。
例:
QThreadPool::globalInstance()->setMaxThreadCount(10);
QFutureSynchronizer<int> synchronizer;
for(int i = 1; i <= 10; i++)
synchronizer.addFuture(QtConcurrent::run(this, &Class1::b));
synchronizer.waitForFinished();
foreach(QFuture<int> thread, synchronizer.futures())
qDebug() << thread.result(); //Get the return value of each thread.
相关文章:
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 在大括号内添加语句会更改代码功能?
- 在 C++ 中向多态树添加功能
- Trie 查找/添加功能无法正常工作
- 简单的二进制搜索树非递归添加功能
- Omnet 在我的班级中添加功能
- 如何正确覆盖虚拟方法以添加功能
- Qthread,为qthread添加功能
- 如何打印添加功能的结果.
- 如果我希望子类向父类添加功能,我是否应该保护我的属性
- 在没有继承的情况下向c++类添加功能
- 向类及其所有子类添加功能
- 用于添加功能的继承
- 如何向派生类添加功能
- 如何强制重新运行cmake,或通过CMakeLists.txt添加功能到makefile
- 为已经存在的函数添加功能(c#, c++)
- 如何修复错误并向源文件添加功能,同时保证不会破坏某些应用程序
- 使用继承添加功能
- 链表需要帮助无法打印我的数据.想要添加功能.在 C - C++ 中
- 在OOP中向现有类添加功能