[Qt-interest] query on QCoreApplication:exec()

Konrad Rosenbaum konrad at silmor.de
Sat Dec 6 08:46:21 CET 2008


On Tuesday 02 December 2008, Prasanta Sadhukhan wrote:
> Prasanta Sadhukhan wrote:
> > /but in the attached application, I tried creating a linux thread from
> > main and tries to call exec from the spawned thread and exec works.
> > I guess as per above line, exec() should only be calling from thread
> > running main() and not from the spawned thread, am I right?
> > My question is is any thread a main thread so long it's not spawned by
> > QThread api?

To keep things clean you SHOULD only call it in the real main thread (the 
one which is started with the process, the one main() is called in).

You MAY at your own risk instantiate and call it in a different thread that 
was not created by QThread. However, you MUST call it in ONLY ONE thread. 
And you MUST create the QCoreApplication object and call exec in the SAME 
thread. If you do use a non-main() thread, the real main() thread will miss 
some features (like Queued Signal Connections) and some code depending on 
QThread::current() may fail (which might be any innocent looking piece of 
Qt-based code).

Also, I recommend you do not mix Qt-threads and operating-system threads - 
it can cause very interesting headaches. While in Qt do as Qt does (ie. use 
QThread exclusively).

Another recommendation would be to heed recommendations, especially while 
you do not exactly know why they were made yet. If you rely on something 
working despite breaking a recommendation you might be in for a very nasty 
surprise with the next release of Qt or the next change you make in a 
critical section of your own code.

> that it will throw an warning as shown above if not called from main
> thread but I did not get any warning, so I guess any thread can call
> exec() so long the thread also does QApplication(argc, argv) and it's
> not spawned by QThread api?
> Can you please confirm?

You did not get a warning, because you tricked QCoreApplication into 
believing it is in the main() thread.



	Konrad
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20081206/3128e718/attachment.bin 


More information about the Qt-interest-old mailing list