Single thread application qt slot executes in which thread

752 views Asked by At

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 :(

2

There are 2 answers

2
Sebastian Lange On BEST ANSWER

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.

  1. 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 the run() function.

  2. Move your calculation into a function and run it using QtConcurrent::run(), which will automatically use a thread on its own.

  3. 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

0
Kuba hasn't forgotten Monica On

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.

Each thread can have an event loop. If your code is not returning the control to the event loop in the thread it runs in, you're not going to be processing any events, and are setting yourself up for failure. So don't do that. Transform your code as follows:

// before
forever {
  code();
}

// after
void timerEvent(QTimerEvent *ev) {
  if (ev->timerId() == m_timer.timerId())
    code();
}

void start() {
  m_timer.start(0, this);
}

QBasicTimer m_timer; // class member