Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QThreadPool example

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 ?

like image 422
CDT Avatar asked Jun 21 '13 13:06

CDT


1 Answers

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.

like image 98
Pavel Strakhov Avatar answered Oct 13 '22 11:10

Pavel Strakhov