关于 QThread 的问题
如果我创建一个 QThread 并从另一个线程调用其插槽之一,那么它是在 QThread 对象的线程上下文中调用,还是从发出调用的线程上下文中调用?
If I make a QThread and call one of its slots from another thread, will it be called in the context of the thread of the QThread object, or from the context of the thread which made the call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您通过发出信号来执行插槽,那么这取决于您所拥有的信号到插槽连接的类型。通过直接连接连接到信号的槽将在发射器的线程中执行。通过排队连接连接的槽将在接收者的线程中执行。请参阅此处:http://doc.qt.nokia.com/4.7/threads -qobject.html
如果直接执行该槽,使用[QThread对象]->slot(),那么该槽将在进行调用的线程中执行。
If you execute the slot by emitting a signal, then it depends on the type of signal-to-slot connection you have. A slot connected to a signal via a direct connection would execute in the emitter's thread. A slot connected via a queued connection would execute in the receiver's thread. Please see here: http://doc.qt.nokia.com/4.7/threads-qobject.html
If the slot is executed directly, with [QThread object]->slot(), then the slot will execute in the thread that makes the call.
直接调用始终在调用线程的上下文中执行。
Direct calls are always executed in the context of the calling thread.
由信号调用的槽将在与 QObject 关联的线程中运行。直接调用的槽将在当前线程中运行。这是一个演示的测试程序。
输出:
测试程序:
A slot called by a signal will run in the thread for which the
QObject
is associated. A slot called directly will run in the current thread. Here is a test program which demonstrates.Output:
Test program: