单线程应用程序qt插槽在哪个线程中执行 [英] Single thread application qt slot executes in which thread

查看:135
本文介绍了单线程应用程序qt插槽在哪个线程中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在一个单线程应用程序中,我创建了一个服务器,并使用如下所示的新连接到达信号连接了一个插槽,

 connect(mTcpServer, SIGNAL(newConnection()), this, SLOT(newClientConnected()));

在这一行之后,我进入了一个巨大的循环,在其中进行一些计算.因此,作为主线程的我的单线程在一个循环中很忙,现在有一个新的连接到达.

我的问题是

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.

我是QT的新手:(

解决方案

主线程.

由于您运行的是单线程应用程序,因此所有操作都将在此进行处理(基本级别的某些IO除外).

您的插槽将始终在调用线程中执行,除非您创建Qt::QueuedConnection以便在拥有该插槽的对象所属的线程中运行该插槽.一旦运行多个线程,这一点就变得很重要.

每个标准QThread都有其自己的事件队列.由于您只有一个线程应用程序,因此事件队列也将在主线程中运行.

这表明在长时间运行的循环中,将不会进行任何事件处理,也不会对新连接进行任何处理.

解决方案:使长时间运行的计算在其他线程中运行,以继续处理新的连接和事件.这里有多种选择供您选择.

  1. 在大多数讨论中有点不赞成,但对于长时间运行的操作(在计算过程中不必处理信号/事件)仍然有效的是将其归类为QThread并重新实现run()函数.

  2. p>
  3. 将您的计算移到一个函数中,然后使用QtConcurrent::run()运行它,它将自动自动使用一个线程.

  4. 创建QRunnable的子类并使用全局线程池.

所有选项均有效,但实现上略有不同.请参阅文档以获取更多详细信息: http://doc.qt.io/qt-5/thread-basics.html

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.

  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

这篇关于单线程应用程序qt插槽在哪个线程中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆