0

I am using QT 5.11.2 on both linux and windows. I am trying to optimize my UI to prevent it from freezing in large functions. I started using QThread and was able to do exactly what I want on windows. However, I tried to test the same functions on linux RHEL7 but threads never finished.

Here is what I tried:

void MainWidget::Configure_BERT_DSO(bool isOptimization, double lineRate, int Scaling)
{
    QThread *bertThread = QThread::create([this, lineRate, Scaling]{ ConfigureBert(lineRate, Scaling); 

    QThread *dsoThread = QThread::create([this, lineRate]{ ConfigureDSO(lineRate); });

    bertThread->setObjectName("My Bert Thread");
    dsoThread->setObjectName("My DSO Thread");

    bertThread->start();
    dsoThread->start();

    while(bertThread->isRunning() || dsoThread->isRunning())  //  even tried isFinished()
    {
        qApp->processEvents();
    }

    bertThread->exit();
    dsoThread->exit();

    delete bertThread;
    delete dsoThread;
}

In windows the while loop exits after a while and both threads execute correctly with no problem. On linux, to make sure that both functions execute correctly, I added qDebug() at the start and end of each function and they are all reached at the excpected time. But the problem is that isRunning never becomes true again and the same goes for isFinished and my loop gets stuck.

Thread: QThread(0x1d368b0, name = "My Bert Thread") started.

Thread: QThread(0x1d368e0, name = "My DSO Thread") started.

Thread: QThread(0x1d368b0, name = "My Bert Thread") finished.

Thread: QThread(0x1d368e0, name = "My DSO Thread") finished.

Is it something platform dependent or is there something that I might be missing?

EDIT I also tried to use bertThread->wait() and dsoThread->wait() to check if it exits after my functions finish but it never returns from the first one I called although both functions reach their end successfully

Your help is much appreciated

DoubleH
  • 3
  • 2
  • Can you add a `qDebug` statement inside the `while(bertThread->isRunning() || ...` loop? How often do you see its associated output? There's no guarantee `qApp->processEvents()` will return as soon as the threads have completed their work. – G.M. Apr 13 '21 at 14:57
  • @G.M. Do you want me to test it on windows? Because in linux the isRunning() is always true hence qDebug will always print an output. As for testing on windows, both threads take about 10 seconds so qDebug gets printed a lot until these threads finish – DoubleH Apr 14 '21 at 05:23
  • But how many times is `isRunning()` called? How do you know the code shown isn't 'stuck' in `qApp->processEvents()` processing some other, unrelated, event? – G.M. Apr 14 '21 at 06:34
  • @G.M. No it's not getting stuck in ```qApp->processEvents()``` . I added the ```qDebug``` in my loop and it kept printing – DoubleH Apr 14 '21 at 06:50

0 Answers0