Suppose in a single threaded application, I have created a server and connected a slot with new connection arrival signal like following,
connect(mTcpServer, SIGNAL(newConnection()), this, SLOT(newClientConnected()));
and after this line I went into a huge loop where I do some calculations. So my single thread which is the main thread is busy in a loop and now a new connection arrives.
So my questions are,
1) In which thread the new slot will be executed? I ask this because
main thread is already executing some code in a loop.
2) In which thread the event loop is maintained? Because certainly my single
thread is executing some code in a loop and is not maintaining the event loop.
I'm a newbie in QT :(
Main Thread.
Since you are running a single threaded application everything will be handled there (except some IO on the base level).
Your slot will always be executed in the calling thread, except you create a
Qt::QueuedConnection
to run the slot in the thread the object owning the slot belongs to. This becomes important as soon as you run multiple threads.Every standard
QThread
has its own event queue. Since you have a single threaded application your event queue will also run in the main thread.This concludes during your long running loop there will be no event processing and no handling for new connections.
Solution: Make your long running calculation run in a different thread to keep handling new connections and events. There are different options for you here.
A bit out of favour in most of discussion but still valid for long running operations that do not have to handle signals/events during their calculation is to subclass
QThread
and reimplement therun()
function.Move your calculation into a function and run it using
QtConcurrent::run()
, which will automatically use a thread on its own.Create a subclass of
QRunnable
and use the global thread pool.All options are valid but a little different in implementation. See the documentation for more detailed information: http://doc.qt.io/qt-5/thread-basics.html