我有一个相互调用的 3 个 QThreads(全部继承自 QThread。我知道有些人可能会建议使用 moveToThread,但暂时忽略这个事实)。简化的代码如下所示:
线程1类:
void
Thread1::run
{
// some execution
Thread2 t2 = new Thread2();
connect(t2,SIGNAL(finished),this,SLOT(onFinished));
t2->start();
while(!stop) // stop was initialized as false
{
this->msleep(10);
}
}
void Thread1::onFinished(){ stop = true; }
线程2类:
void
Thread2::run
{
// some execution
Thread3 t3 = new Thread3();
connect(t3,SIGNAL(finished),this,SLOT(onFinished));
t3->start();
while(!stop) // stop was initialized as false
{
this->msleep(10);
}
}
void Thread2::onFinished(){ stop = true; }
线程3类:
void
Thread3::run
{
// some execution
QMutexLocker ml(&mMutex);
}
当我只有两个线程时,它工作得非常好(例如只有 thread2 和 thread3)。在我转移到三线程场景之后,onFinished() 方法似乎不再正确地连接到 finished() 信号。thread2 中的 onFinished() 曾经被调用过。而且我很确定thread3的执行已经完成。
谁能告诉我哪里做错了?