Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    How to make synchronous function from a asynchronous function without QEventLoop

    General and Desktop
    6
    10
    4679
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • X
      XelaG last edited by

      Hello,

      I'm pretty new to Qt and I would like to synchronously wait for an asynchronous function. So I used a QEventLoop object with its quit() slot connected to a finished() signal. But according to some people this should not be use in a "real app" even with a QTimer to prevent dead lock (in case of the signal is emitted before entering the function QEventLoop::exec() ).
      I don't want to write an active loop that will check a flag's state cause it will consume all the CPU time.

      Most of the examples found on the web use a QEventLoop object.

      So how can I make to wait for the end of an asynchronous function without consuming all of the CPU time, without using a QEventLoop object and without block the GUI ?

      Thanks
      Alex

      "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Martin Golding

      1 Reply Last reply Reply Quote 0
      • D
        DBoosalis last edited by

        Why don't you put your function call in a QThread

        1 Reply Last reply Reply Quote 0
        • X
          XelaG last edited by

          Cause I will have to wait for the thread to achieve the tasks, which is the same problem. I will have to wait in the main thread.

          For example, I have to initialize a device connected using a bluetooth link. So I have to discover which bluetooth services are available (which is made asynchronously), connect to a bluetooth socket (which is made asynchronously) and then I can send and receive data to/from the device.
          So I must wait for all of these initialization steps to finish.

          I don't want to use several slots to do so if I can simply do, which is much more convenient.
          @if( ptDevice->open() )
          {
          // do some stuff
          }
          @

          What the point of dispatching it into several slots if we can do it in a single function ? Sometimes iIt complicates the code for no reasons.
          Some tasks have to be done synchronously.

          "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Martin Golding

          1 Reply Last reply Reply Quote 0
          • A
            andreyc last edited by

            You can synchronize the threads using "QWaitCondition":http://qt-project.org/doc/qt-5/qwaitcondition.html#details
            Here is an "example":http://qt-project.org/doc/qt-5/qtcore-waitconditions-example.html

            1 Reply Last reply Reply Quote 0
            • X
              XelaG last edited by

              Thanks guys for your replies and your time.

              But I won't create a thread for each task than can be done synchronously that make no sense. My app will have hundred of threads or hundreds of slots for no good reasons. Debugging it will turn into a nightmare.

              I'll find another solution, I don't believe it can't be done with Qt. It can be done with Delphi, Borland C++ and Visual C++.

              Thanks.

              "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Martin Golding

              1 Reply Last reply Reply Quote 0
              • A
                ablasche last edited by

                Assume you have two objects with a start() function and finished() signal. You want to start() the second object once you have received the finished() signal from the first object. Below is some pseudo code.

                @
                void MyClass::setup() {
                A *a = new A();
                B *b = new B();
                connect(a, SIGNAL(finished()), this, SLOT(startB()));
                connect(b, SIGNAL(finished()), this, SLOT(chainComplete()));
                }

                void MyClass::runChain() {
                a->start();
                }

                void MyClass::startB() {
                b->start();
                }

                void MyClass::chainComplete() {
                qDebug() << "Chain has completed";
                }
                @

                You can extend this pattern until you have reached the desired chain. Essentially the next step is executed once the end signal from the previous object was received.

                --
                Alex

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  Wouldn't then a state machine be more clean ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply Reply Quote 0
                  • A
                    ablasche last edited by

                    By all means, you can do that too. I just wanted to demo the main principle.

                    A state machine becomes more efficient the longer the chain is.

                    --
                    Alex

                    1 Reply Last reply Reply Quote 0
                    • X
                      XelaG last edited by

                      [quote author="Alex Blasche" date="1404976000"]You can extend this pattern until you have reached the desired chain. Essentially the next step is executed once the end signal from the previous object was received.[/quote]
                      Thanks Alex, I know how to chain slots and signals but if I do that in my case I'll have tons of slots, for no good reasons to me.

                      [quote author="SGaist" date="1404978056"]Wouldn't then a state machine be more clean ?[/quote]
                      Definitely.

                      "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Martin Golding

                      1 Reply Last reply Reply Quote 0
                      • jeremy_k
                        jeremy_k last edited by

                        [quote author="XelaG" date="1404974539"]
                        I'll find another solution, I don't believe it can't be done with Qt. It can be done with Delphi, Borland C++ and Visual C++.
                        [/quote]

                        I think there's some ambiguity about what is meant by an asynchronous function. Maybe with an example and an illustration of a solution in Delphi, Borland C++ or MS Visual C++, someone can translate it into something that works with Qt. Qt is after all largely a C++ framework.

                        Asking a question about code? http://eel.is/iso-c++/testcase/

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post