QThreadPool Example

I am looking for a somewhat concise example of using QThreadPool . Here is 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 good practice?
PS: I saw waitForDone in the link, when should I call waitForDone ?

+4
source share
1 answer

This is almost correct with one exception. QRunnable not a thread, and you should not call your class MyThread . MyRunnable or MyTask or rather.

Please note that your code is almost the same as in the example on the documentation page. Documentation is the best source for brief examples.

You must call waitForDone when you want to wait until all handlers are processed. It depends on your application architecture. This is usually when you created and launched all the QRunnable and want to use their results.

+4
source

Source: https://habr.com/ru/post/1487472/


All Articles