Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to (properly) check if ALL started QThread workers finished their tasks



  • I haven't used multithreading (especially QThreads) that often before and I'm wondering how to check if all started worker QThreads are finished() to continue processing the result from my threads in my GUI class.
    I came across QThreadPool::waitForDone() but as far as I understand, it only works with QRunnable?!
    In plain C++ I would just do something like std::thread->join() on all threads.

    Currently I'm using the classic "worker QObject + moveToThread()" approach with multiple workers /QThreads and I don't want to change it for now.

    Thanks in advance :)


  • Lifetime Qt Champion

    Hi,

    AFAIR, the equivalent would be wait however it's blocking and likely not what you want.

    You should rather have a counter that you decrement each time a task is done and then do the rest once it reached zero.



  • @SGaist said in How to (properly) check if ALL started QThread workers finished their tasks:

    AFAIR, the equivalent would be wait however it's blocking and likely not what you want.

    No :)

    You should rather have a counter that you decrement each time a task is done and then do the rest once it reached zero.

    A counter as member variable in my MainWindow class will work?!

    So I connect finished() to an additional slot which will decrease my member?


  • Lifetime Qt Champion

    That would be one way yes, if you spawn all your threads there.

    Depending on what you want to do you might want to consider modeling the execution using a state machine.



  • Hi,

    You can connect the "finished()" signal to a slot and there check if all threads are not running. I assume that you have a list of threads.