【发布时间】:2016-12-23 10:18:07
【问题描述】:
我有三个 QThread 运行三个事件循环。
在每个线程中,我有一个对象,这些对象有信号和槽。
- ThreadA => ObjectA 与 theSignalA 和 theSlotA
- ThreadB => ObjectB 与 theSignalB 和 theSlotB
- ThreadC => 带有信号 B 和插槽 B 的对象 C
我完全理解一个信号和一个插槽之间的连接是如何工作的:
// This connection would execute theSlotB in ThreadA
connect(objectA, SIGNAL(theSignalA()), objectB, SLOT(theSlotB()), Qt::DirectConnection);
// This connection would post an event in ThreadB event loop, which will execute theSlotB
connect(objectA, SIGNAL(theSignalA()), objectB, SLOT(theSlotB()), Qt::QueuedConnection);
我要问的是这样的事情的行为:
auto sig2sig = Qt::DirectConnection;
auto sig2slot = Qt::DirectConnection;
connect(objectA, SIGNAL(theSignalA()), objectB, SIGNAL(theSignalB()), sig2sig);
connect(objectB, SIGNAL(theSignalB()), objectC, SLOT(theSlotC()), sig2slot);
对于sig2sig 和sig2slot 的不同可能值,在theSlotC 执行的位置(和时间)。
-
sig2sig = DirectConnection,sig2slot= DirectConnection,- 作为
theSignalA和theSignalB之间的直接连接?
- 作为
-
sig2sig = DirectConnection,sig2slot= QueuedConnection,- 作为
theSignalA和theSignalB之间的QueuedConnection ?
- 作为
-
sig2sig = QueuedConnection,sig2slot= DirectConnection,-
theSlotC是否在ThreadB中执行?
-
-
sig2sig = QueuedConnection,sig2slot= QueuedConnection,-
theSlotC是否在ThreadC中执行,但在重新发出来自ThreadB的信号的延迟之后?
-
或者也许信号/信号连接被丢弃了?
【问题讨论】:
-
连接的目标之间绝对没有区别:它们只是一种方法。该方法是由您编写的,还是由
moc编写的,都无关紧要。行为是相同的。您还应该注意,很少有理由不使用自动连接类型。 -
你能详细说明这几个原因吗?
-
如果您打算从事件循环中调用目标,即使它具有相同的线程上下文,您可能希望强制排队连接。如果目标方法是线程安全的,您可能希望强制直接连接到位于不同线程中的目标。您可能很少需要阻塞队列连接。就是这样。
标签: c++ multithreading qt