Multiple calls to QCoreApplication::quit()
-
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!
-
@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.
-
@kshegunov So while the destructor isn't called, calling QCoreApplication::quit() multiple times is safe?
-
Yes, that's correct.
-
@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)
-
@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 forexit(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#1421Even 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()
beforeQCoreApplication::exec
in which case callingquit()
does nothing. If you expect that you should be callingquit()
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. -
@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);
-
@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.