Reputation: 145
I have a class like that
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
}
how can I use qhthread here,I ve looked to tutorials they all are just for classes not for a function?
Upvotes: 4
Views: 1182
Reputation: 2881
If you need to run a function on a separate thread, you can use QtConcurrent
like this:
QtConcurrent::run(this, &Class1::b);
Edit: You can use QFutureSynchronizer
to wait for all threads, don't forget to allocate all threads using QThreadPool::globalInstance()->setMaxThreadCount()
.
Edit 2: You can use synchronizer.futures()
to acess all threads return values.
Example:
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.
Upvotes: 4