Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPromise/QFuture and QMetaObject::invokeMethod
Forum Updated to NodeBB v4.3 + New Features

QPromise/QFuture and QMetaObject::invokeMethod

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 432 Views 2 Watching
  • 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    I'm trying to do this >

        QFuture<int> icServerHTTP::startServer(const quint16 port) noexcept {
            QPromise<int> promise;
            if (thread() != QThread::currentThread()) {
                QFuture<int> future = promise.future();
                QMetaObject::invokeMethod(this, [this, port, promise = std::move(promise)]() mutable {
                    try {
                        int result = _startServer(port);
                        promise.addResult(result);
                        promise.finish();
                    } catch (...) {
                        promise.setException(std::current_exception());
                    }
                }, Qt::QueuedConnection);
                return future;
            }
            promise.addResult(_startServer(port));
            promise.finish();
            return promise.future();
        }
    

    I tried wrapping it in smart pointer, but that cause >
    db130455-f0a9-41b7-9fe0-0a43a48ba5dd-image.png
    Due to memory issues and smart pointer cleaning the obj instead of the future/etc doing it... Ahh tricky...

    Any idea how to make it work?
    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly are you trying to put in a smart pointer ?
      Which one is it ?
      How are you trying to do it ?

      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
      0
      • D Offline
        D Offline
        Dariusz
        wrote on last edited by Dariusz
        #3

        I tried to put promise in smart pointer.
        In general I cant pass promise to invokeMethod. The concept is that if I run in correct thread, I want it to just run normally, but if not, I need it to execute in correct thread, but I still want to be able to wait for it to finish/get correct result out of it. Atm this crashes.
        I keep hitting those issues more and more, asking it to execute in another thread/etc/etc... :/

        You can run the example above and just instead of putting _startServer(port) just set int to 20 or something like that. But u need to simulate InvokeMethod, I guess you can just run invoke method regardless/ignore thread check to reproduce the problem... Like this >

                QPromise<int> promise;
                QFuture<int> future = promise.future();
                QMetaObject::invokeMethod(this, [this, port, promise]() mutable {
                    try {
                        // Call startServer on the correct thread
                        int result = 10;
                        // Set the result to the promise
                        promise.addResult(result);
                        promise.finish();
                    } catch (...) {
                        // Handle any exceptions and set as failure in promise
                        promise.setException(std::current_exception());
                    }
                }, Qt::QueuedConnection);
        
                return future;
        
        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dariusz
          wrote on last edited by
          #4

          Ehh ok I got it working.

          Turns out I can't call future.result(), before it actually is finished... and I need to fo isFinished/waitForFinished before checking it sigh.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Isn't QFuture::unwrap what you are looking for ?

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

            D 1 Reply Last reply
            0
            • SGaistS SGaist

              Isn't QFuture::unwrap what you are looking for ?

              D Offline
              D Offline
              Dariusz
              wrote on last edited by
              #6

              @SGaist No, in this case the QFuture was crashing when I called for result before it was finished. I expected it to have safeguard internally to check if its done/finished instead of trying to access invalid data/crash.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                In that case, shouldn't you be using the then method to call your custom function ?

                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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved