CDT CDT - 10 months ago 242
C++ Question

QThreadPool example

I'm looking for some concise example on the use of

. Here's how I used it:

QThreadPool *thread_pool = QThreadPool::globalInstance();
MyThread *thread = new MyThread();

class MyThread : public QRunnable {
void run();

void MyThread::run()
qDebug() << "MyThread";

Is the above the right practice ?

PS: I saw
in the reference, when should I call


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 QRunnables and want to use their results.