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. Multiple calls to QCoreApplication::quit()

Multiple calls to QCoreApplication::quit()

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 949 Views 1 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.
  • T Offline
    T Offline
    Teodor Stefan
    wrote on last edited by
    #1

    Hello!

    Sorry for the newbie question, but I cannot find a straight-forward answer

    Here is a quote from the documentation: "Thread-safety note: this function may be called from any thread to thread-safely cause the currently-running main application loop to exit. However, thread-safety is not guaranteed if the QCoreApplication object is being destroyed at the same time."

    What about calling QCoreApplication::quit() multiple times ? Regardless of which thread(s) make the calls. Is it safe? Is it defined behavior? Will it cause a crash if the timing is not right?

    I tried writing a test app where I call QCoreApplication::quit() multiple times from different threads and everything seemed fine.

    Thank you!

    kshegunovK 1 Reply Last reply
    0
    • T Teodor Stefan

      Hello!

      Sorry for the newbie question, but I cannot find a straight-forward answer

      Here is a quote from the documentation: "Thread-safety note: this function may be called from any thread to thread-safely cause the currently-running main application loop to exit. However, thread-safety is not guaranteed if the QCoreApplication object is being destroyed at the same time."

      What about calling QCoreApplication::quit() multiple times ? Regardless of which thread(s) make the calls. Is it safe? Is it defined behavior? Will it cause a crash if the timing is not right?

      I tried writing a test app where I call QCoreApplication::quit() multiple times from different threads and everything seemed fine.

      Thank you!

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @Teodor-Stefan said in Multiple calls to QCoreApplication::quit():

      However, thread-safety is not guaranteed if the QCoreApplication object is being destroyed at the same time.

      This simply refers to the object destructor.

      Read and abide by the Qt Code of Conduct

      T 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @Teodor-Stefan said in Multiple calls to QCoreApplication::quit():

        However, thread-safety is not guaranteed if the QCoreApplication object is being destroyed at the same time.

        This simply refers to the object destructor.

        T Offline
        T Offline
        Teodor Stefan
        wrote on last edited by
        #3

        @kshegunov So while the destructor isn't called, calling QCoreApplication::quit() multiple times is safe?

        kshegunovK 1 Reply Last reply
        0
        • T Teodor Stefan

          @kshegunov So while the destructor isn't called, calling QCoreApplication::quit() multiple times is safe?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          Yes, that's correct.

          Read and abide by the Qt Code of Conduct

          T 1 Reply Last reply
          2
          • kshegunovK kshegunov

            Yes, that's correct.

            T Offline
            T Offline
            Teodor Stefan
            wrote on last edited by
            #5

            @kshegunov . I forgot to mention something important. I am using Qt version 5, not 6.

            So the question now is. In Qt 5 (where QCoreApplication::quit() is equivalent to QCoreApplication::exit(0) ), is it safe to call QCoreApplication::quit from different threads other than the QApp's thread ? Even multiple times? Or is it undefined behavior? The docs seem to recommend invoking quit from the QApp's thread (https://doc.qt.io/qt-5/qcoreapplication.html)

            kshegunovK 1 Reply Last reply
            0
            • T Teodor Stefan

              @kshegunov . I forgot to mention something important. I am using Qt version 5, not 6.

              So the question now is. In Qt 5 (where QCoreApplication::quit() is equivalent to QCoreApplication::exit(0) ), is it safe to call QCoreApplication::quit from different threads other than the QApp's thread ? Even multiple times? Or is it undefined behavior? The docs seem to recommend invoking quit from the QApp's thread (https://doc.qt.io/qt-5/qcoreapplication.html)

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              @Teodor-Stefan said in Multiple calls to QCoreApplication::quit():

              @kshegunov . I forgot to mention something important. I am using Qt version 5, not 6.

              So the question now is. In Qt 5 (where QCoreApplication::quit() is equivalent to QCoreApplication::exit(0) )

              They are equivalent in Qt6 as well. quit() is a shorthand for exit(0).

              is it safe to call QCoreApplication::quit from different threads other than the QApp's thread ?

              Looks like it, yes (the implementation is the same in Qt6):
              https://codebrowser.dev/qt5/qtbase/src/corelib/kernel/qcoreapplication.cpp.html#1421

              Even multiple times?

              Most certainly. Calling quit just marks a flag that the event loop(s) should exit.

              The docs seem to recommend invoking quit from the QApp's thread (https://doc.qt.io/qt-5/qcoreapplication.html)

              I don't see it. The docs simply refer to you possibly calling quit() before QCoreApplication::exec in which case calling quit() does nothing. If you expect that you should be calling quit() before the event loop has started, then it is necessary to queue the call through the event loop. That way you're certain that the application will actually quit when it comes to process the events.

              Read and abide by the Qt Code of Conduct

              T 1 Reply Last reply
              2
              • kshegunovK kshegunov

                @Teodor-Stefan said in Multiple calls to QCoreApplication::quit():

                @kshegunov . I forgot to mention something important. I am using Qt version 5, not 6.

                So the question now is. In Qt 5 (where QCoreApplication::quit() is equivalent to QCoreApplication::exit(0) )

                They are equivalent in Qt6 as well. quit() is a shorthand for exit(0).

                is it safe to call QCoreApplication::quit from different threads other than the QApp's thread ?

                Looks like it, yes (the implementation is the same in Qt6):
                https://codebrowser.dev/qt5/qtbase/src/corelib/kernel/qcoreapplication.cpp.html#1421

                Even multiple times?

                Most certainly. Calling quit just marks a flag that the event loop(s) should exit.

                The docs seem to recommend invoking quit from the QApp's thread (https://doc.qt.io/qt-5/qcoreapplication.html)

                I don't see it. The docs simply refer to you possibly calling quit() before QCoreApplication::exec in which case calling quit() does nothing. If you expect that you should be calling quit() before the event loop has started, then it is necessary to queue the call through the event loop. That way you're certain that the application will actually quit when it comes to process the events.

                T Offline
                T Offline
                Teodor Stefan
                wrote on last edited by
                #7

                @kshegunov Thank you for your reply! However I would like to add that the implementations for QCoreApplication::quit() are different between Qt5 and Qt6.

                In Qt5 it simply calls exit(0) from the current thread.

                In Qt6 it seems to post the 'quit' event so that it makes sure the subsequent exit() will run on the main thread.

                void QCoreApplication::quit()
                {
                    if (!self)
                        return;
                    self->d_func()->quit();
                }
                void QCoreApplicationPrivate::quit()
                {
                    Q_Q(QCoreApplication);
                    if (QThread::currentThread() == mainThread()) {
                        QEvent quitEvent(QEvent::Quit);
                        QCoreApplication::sendEvent(q, &quitEvent);
                    } else {
                        QCoreApplication::postEvent(q, new QEvent(QEvent::Quit));
                    }
                }
                

                This is why I thought the example from the documentation in Qt5 seems to recommend executing the QCoreApplication::quit() slot on the app's primary thread:

                connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection);
                
                kshegunovK 1 Reply Last reply
                1
                • T Teodor Stefan

                  @kshegunov Thank you for your reply! However I would like to add that the implementations for QCoreApplication::quit() are different between Qt5 and Qt6.

                  In Qt5 it simply calls exit(0) from the current thread.

                  In Qt6 it seems to post the 'quit' event so that it makes sure the subsequent exit() will run on the main thread.

                  void QCoreApplication::quit()
                  {
                      if (!self)
                          return;
                      self->d_func()->quit();
                  }
                  void QCoreApplicationPrivate::quit()
                  {
                      Q_Q(QCoreApplication);
                      if (QThread::currentThread() == mainThread()) {
                          QEvent quitEvent(QEvent::Quit);
                          QCoreApplication::sendEvent(q, &quitEvent);
                      } else {
                          QCoreApplication::postEvent(q, new QEvent(QEvent::Quit));
                      }
                  }
                  

                  This is why I thought the example from the documentation in Qt5 seems to recommend executing the QCoreApplication::quit() slot on the app's primary thread:

                  connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection);
                  
                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @Teodor-Stefan said in Multiple calls to QCoreApplication::quit():

                  In Qt6 it seems to post the 'quit' event so that it makes sure the subsequent exit() will run on the main thread.

                  I must've completely missed that.

                  Read and abide by the 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