Reputation: 10661
I'm looking for some concise example on the use of QThreadPool
. Here's how I used it:
QThreadPool *thread_pool = QThreadPool::globalInstance();
MyThread *thread = new MyThread();
thread_pool->start(thread);
class MyThread : public QRunnable {
public:
MyThread();
~MyThread();
void run();
};
void MyThread::run()
{
qDebug() << "MyThread";
}
Is the above the right practice ?
PS: I saw waitForDone
in the reference, when should I call waitForDone
?
Upvotes: 6
Views: 14035
Reputation: 40512
This is almost correct with one exception. QRunnable
is not a thread, and you should not call your class MyThread
. MyRunnable
or MyTask
is more correct.
Note that your code is almost the same as the example on the documentation page. The documentation is the best source of concise examples.
You should call waitForDone
when you want to wait until all runnables are processed. It depends on your app's architecture. Usually it's when you created and started all QRunnable
s and want to use their results.
Upvotes: 7