[Interest] QThreadPool wait until all queued threads are finished

Thiago Macieira thiago.macieira at intel.com
Tue Aug 17 22:14:09 CEST 2021


On Tuesday, 17 August 2021 12:01:25 PDT Scott Bloom wrote:
> std::list< yyy > xxx;
> for( auto && ii : xxx )
> {
>      QThreadPool::globalInstance()->start(
>         [ii]()
>         {
>             doSomething( ii );
>         } );
> }
> 
> Works great for this... However, I want the application waiting until all
> threads are completed before exiting the tool.
> 
> while (QThreadPool::globalInstance()->activeThreadCount())
> {
>         QThreadPool::globalInstance()->waitForDone(100);
>         qApp->processEvents();
> }

Busy-waiting is never a good idea.

You should use QFuture/QFutureWatcher and react to the latter's signals 
indicating their jobs are done.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering





More information about the Interest mailing list