[Interest] QThreadPool wait until all queued threads are finished

Scott Bloom scott at towel42.com
Tue Aug 17 21:15:40 CEST 2021


Im really trying to avoid making my own thread pool.  QTHreadPool works great except there is nothing I can do to wait for all queued to finish as well.

Scott

From: Jérôme Godbout <jgodbout at dimonoff.com>
Sent: Tuesday, August 17, 2021 12:13
To: Scott Bloom <scott at towel42.com>; interest at qt-project.org
Subject: Re: QThreadPool wait until all queued threads are finished

You should make a QThreadPool for your need and call the QThreadPool::waitForDone() until all you started is completed.

Other side note to help if the above solution is not enough:
You could connect to each QThread finished() event and check if all thread has finished. You can check if the QThread is into the QThreadPool.contains(QThread*), but I do not have a solution to have all the QThread* that are running?! It's kind of odd that the QThreadPool doesn't return  a QThread when you start one or could at least iterate over them.


Jérôme Godbout, B. Ing.

Software / Firmware Team Lead
O: (418) 682-3636 ext.: 114
C: (581) 777-0050
godboutj at dimonoff.com<mailto:godboutj at dimonoff.com>
[signature_2038249735]<https://www.dimonoff.com/>
dimonoff.com<https://www.dimonoff.com/>
1015 Avenue Wilfrid-Pelletier,
Québec, QC G1W 0C4, 4e étage


From: Interest <interest-bounces at qt-project.org<mailto:interest-bounces at qt-project.org>> on behalf of Scott Bloom <scott at towel42.com<mailto:scott at towel42.com>>
Date: Tuesday, August 17, 2021 at 3:01 PM
To: interest at qt-project.org<mailto:interest at qt-project.org> <interest at qt-project.org<mailto:interest at qt-project.org>>
Subject: [Interest] QThreadPool wait until all queued threads are finished
Here is the problem Im having.  I have background task, that is small enough to use a lambda on a given string.

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();
}

Should work.  Wait 100ms, and continue.

However, the problem Im having, is activeThreadCount does not take into account queued threads.

Is there a better way?  The length of the xxx can be in the millions, and is recursive.

Scott
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210817/380bf2f6/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 9397 bytes
Desc: image002.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210817/380bf2f6/attachment.png>


More information about the Interest mailing list