Search code examples
c++qtembedded-linuxqthread

QThread stuck in second run


I have a QThread on my embedded device. every time I run the application my thread stuck after second run. I try to kill my thread after first run. Still device stuck after second run. I couldn't run my thread correctly.

Here is my code;

void ThreadCurrency::run()
{
    QMutex mutex;
    mutex.lock();
    if(this->CurrencyStop == true)
    {
         mutex.unlock();
        return;
    }

    QByteArray strdata;

    // Create QProcess object
    processCurrency = new QProcess();
    processCurrency->start("curl --insecure -v --cacert /data/ca/cert.pem https://secure.*******************/fx.jsp");

    if (processCurrency->waitForStarted(-1))
    {
        while(processCurrency->waitForReadyRead(-1))
        {
            strdata += processCurrency->readAllStandardOutput();
        }

        QMessageBox msgBox1;
        msgBox1.setWindowTitle("eCode Read");
        msgBox1.setText(strdata);
        msgBox1.exec();
    }
    else
    {
        while(processCurrency->waitForReadyRead(-1))
        {
            strdata += processCurrency->readAllStandardError();
        }

        QMessageBox msgBox1;
        msgBox1.setWindowTitle("eCode Error");
        msgBox1.setText(strdata);
        msgBox1.exec();

    }

    mutex.unlock();
    sleep(1);
    //*****************************************************************
    emit CurrencyChanged(aGBP, aEUR, aUSD, sGBP, sEUR, sUSD);
}

** The Output shows in a json format:**

{
 "date": "20171107", "currency": {
    "dolar": {
        "buy": "3,8200",
        "sale": "3,9050",
        "e_sale": "3,8200"
    },

  }
}

Thank you for suggestion. The QMutexLocker can’t work in my embedded device. QMutex and the QThread is the closes I can get in my device. My problem is the below line: emit CurrencyChanged(aGBP, aEUR, aUSD, sGBP, sEUR, sUSD); The line runs but It doesn’t fire onCurrencyChanged SLOT. Anything in onCurrencyChanged doesn’t run. My main thread code is:

currencyThread = new ThreadCurrency (this);
connect(currencyThread,SIGNAL(CurrencyChanged(QString, QString, QString, QString, QString, QString)), this, SLOT(onCurrencyChanged (QString, QString, QString, QString, QString, QString)));
currencyThread->CurrencyStop = false;

currencyTimer = new QTimer(this);
connect(currencyTimer, SIGNAL(timeout()),this, SLOT(showCurrencyStatus()));
currencyTimer->start(30000); 


void MainWindow::onCurrencyChanged(QString aGBP, QString aEUR, QString aUSD, QString sGBP, QString sEUR, QString sUSD)
{
    // SHOW Currency
    ui->lblALIS_STG->setText(aGBP);
    ui->lblALIS_EUR->setText(aEUR);
    ui->lblALIS_USD->setText(aUSD);
    QCoreApplication::processEvents();

}

Solution

  • 1) You are not allowed to access Widgets in a thread besides the main thread. Thus move all QMessageBox code to the main thread (e.g. after receiving the CurrencyChanged signal).

    2) Make sure, that the CurrencyChanged signal is connected using a QueuedConnection, otherwise, your GUI will be executed inside the secondary worker thread.

    3) Do not use QMutex directly, use a QMutexLocker instead

    4) Why do you need the Mutex anyhow? Locking based on (arbitrary) input thus arbitrary time is close to deadlocking your application.