Unable to connect between QThread with a completed () signal for multiple Qthread

I have 3 QThreads that call each other (all are inherited from QThread. I know that some may suggest using moveToThread, but just ignore this fact for now). The simplified code is as follows:

Thread1 class:

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; }

Thread2 Class:

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; }

Thread3 Class:

void
Thread3::run
{
    // some execution
    QMutexLocker ml(&mMutex);
}

When I have only two threads, it works fine (for example, only thread2 and thread3). The onFinished () method seems to connect to the ready () signal no longer works after I switched to a three-thread scenario. OnFinished () in thread2 has ever been called. And I am sure that the execution of thread3 is complete.

Can someone tell me where I could do wrong?

+4
1

, , - Qt::AutoConnection. , , , Qt::QueuedConnection. : The slot is invoked when control returns to the event loop of the receiver thread. The slot is executed in the receiver thread. , .

2 , , , , . , thread2 thread3, thread2 , thread3 , thread2. , thread2 .

3 thread1 , thread2 , thread1, , thread2 .

QThread:: exec() QThread::run(), , , QThread , , . - QThread. QObject .

Qt::DirectConnection .

+6

All Articles