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
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With