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

How do you wait for multiple signals?



  • I need to make multiple http request in parallel (using QNetworkaAcessManager) and continue the program only after all the requests have finished.

    One possible solution would be to keep track of the total number of requests and the current number of finished request, when the finished() signal is sent increment currentFinishedRequests by one, if it's equal to totalNumberOfRequests it means all the requests have finished (we could send another signal to continue the program or call a function).
    I found this solution a bit ulgy, is there a better way to do it? https://showbox.bio/ https://tutuapp.uno/ https://vidmate.cool/



  • @AntonioFinn

    Please keep in mind that QNetworkAccessManager has a limitation on the number of parallel requests. From documentation:

    Note: QNetworkAccessManager queues the requests it receives. The number of requests executed in parallel is dependent on the protocol. Currently, for the HTTP protocol on desktop platforms, 6 requests are executed in parallel for one host/port combination.



  • The direct answer to your question seems trivial....Ummm maybe a counter that increases and st checked every time the slot code is executed?

    Guess I should have completely read the OP...That's the way I'd do it.



  • Since the time each request takes to be finished is different for each request and therefore the order is kind of random as a matter of principle you never know if the request that finished is the last one. Therefore there is no way around to wait for all the finish signals to arrive. And as you already suggested the easiest implementation is counting them. Just make sure to use an atomic to avoid race conditions for the counter.



  • @gde23 said in How do you wait for multiple signals?:

    Just make sure to use an atomic to avoid race conditions for the counter.

    Is that really necessary? Are the slots truly executed concurrently?


  • Qt Champions 2017

    @AntonioFinn said in How do you wait for multiple signals?:

    One possible solution would be to keep track of the total number of requests and the current number of finished request, when the finished() signal is sent increment currentFinishedRequests by one, if it's equal to totalNumberOfRequests it means all the requests have finished (we could send another signal to continue the program or call a function).
    I found this solution a bit ulgy, is there a better way to do it?

    Well, it is ugly, sort of, but it's a proper one. That's how I'd do it, although I'd encapsulate the implementation in a separate class (a QObject subclass) with a nice to use API.

    @Kent-Dorfman said in How do you wait for multiple signals?:

    Just make sure to use an atomic to avoid race conditions for the counter.

    Is that really necessary?

    Almost certainly not.

    Are the slots truly executed concurrently?

    Not unless you specifically do so manually (i.e. force Qt::DirectConnection)


  • Moderators

    @AntonioFinn said in How do you wait for multiple signals?:

    I found this solution a bit ulgy, is there a better way to do it?

    I would actually store pointers to the QNetworkReply
    the finished signal points to the same reply that you get a pointer to when you make your get request. And you have to delete it manually anyway and you know exactly what request finished when🤷‍♂️


Log in to reply