Real Time Operation - QThread?
-
Hello!
Well I am today need to launch a thread that is updating the values of some variables passed by reference. They are temperature measurements I need that they are updating the screen, on a label and a slider.
I can not get a solution.
By giving start a thread I send a signal to a function that is listening to thread?
Create a function in infinite loop?
I think the first idea of the signal is valid, but i can't implement it. Signs in QT for me are still a mystery, I use, but do not know what is the best way to use them.
That's my thread class:TemperaturesThread::TemperaturesThread(double *tempMesa, double *tempExtr, Repetier *printer) { this->extrudersTemp = tempExtr; this->bedTemp = tempMesa; this->printer = printer; } void TemperaturesThread::run() { for(int i = 0; i < printer->getNoOfExtruders();i++) { extrudersTemp[i]=printer->getExtruderTemp(i); } (*bedTemp)=printer->getBedTemp(); }
Any tips?
Thanks! -
You'd better emit a signal whenever a new measurement is available and then update your UI. The way you're doing it will very likely raise concurrency errors. Try something like this:
void TemperaturesThread::run() { QList<double> extrudersTemp; for(int i = 0; i < printer->getNoOfExtruders();i++) { extrudersTemp.append(printer->getExtruderTemp(i)); } emit this->updateTemp(extrudersTemp, printer->getBedTemp()); }
Then you connect your main thread to this signal. It's much safer.
-
You'd better emit a signal whenever a new measurement is available and then update your UI. The way you're doing it will very likely raise concurrency errors. Try something like this:
void TemperaturesThread::run() { QList<double> extrudersTemp; for(int i = 0; i < printer->getNoOfExtruders();i++) { extrudersTemp.append(printer->getExtruderTemp(i)); } emit this->updateTemp(extrudersTemp, printer->getBedTemp()); }
Then you connect your main thread to this signal. It's much safer.
@Leonardo Eu consegui fazer alguma coisa com a solução que você me passou. Mas estou com esse erro persistindo.
/home/lays/Dropbox/Simple-Projects/Br-Print/GUI-BRPrint/Pandora/temperaturesthread.cpp:-1: error: undefined reference to `TemperaturesThread::updateTemp(QList<double>, double)'
Mas o sinal está declarado corretamente. eu acho...
Segue as classes:
.h file#ifndef TEMPERATURESTHREAD_H #define TEMPERATURESTHREAD_H #include <QObject> #include <QThread> #include "KI/Repetier.h" #include "brprint3d.h" class TemperaturesThread : public QThread { Q_OBJECT private: Repetier *printer; public: TemperaturesThread(Repetier *printer); void run() Q_DECL_OVERRIDE; signals: void updateTemp(QList<double> extrudersTemp, double tempBed); }; #endif // TEMPERATURESTHREAD_H
-
@Lays147 Hi, please keep the thread in english, if you'd like to get some help in your mother thong, there's the dedicated sub forums.
Back to your problem, how are you reading the temperature ? At regular interval ? Do you use a specific driver ?
-
@Lays147 Hi, please keep the thread in english, if you'd like to get some help in your mother thong, there's the dedicated sub forums.
Back to your problem, how are you reading the temperature ? At regular interval ? Do you use a specific driver ?
@SGaist sorry. I will keep the thread in english.
I was able to solve several problems with the help of leandro. But now I face this new problem.
In the code below to use the commented lines:
//mutexIno.lock();
//mutexIno.unlock();
The program crash.
If I use that:
QMutexLocker (& mutexIno)
The program works, but if I do the same action button around 4x the program also gives crash.
Can anyone tell me why?void BrPrint3D::on_bt_table_clicked(bool checked) { //QMutexLocker locker(&mutexIno); //mutexIno.lock(); if(checked==true) { ui->bt_table->setStyleSheet("background-color:red;"); float temp=ui->tb_Table_Temperature->text().toFloat(); printer_object->setBedTemp(temp)==true ? qDebug() <<"OK": qDebug()<<"~OK"; } else { ui->bt_table->setStyleSheet(""); printer_object->setBedTemp(0); } // mutexIno.unlock(); }
-
It is tough to say anything why not all the code is present in QThread
case. It is very sensitive to usage.I would recommend reading sorses on the web, like :
http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/ -
It is tough to say anything why not all the code is present in QThread
case. It is very sensitive to usage.I would recommend reading sorses on the web, like :
http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/@alex_malyu code of my thread
#include "temperaturesthread.h" TemperaturesThread::TemperaturesThread(Repetier *printer) { this->printer = printer; this->stopLoop = false; } void TemperaturesThread::run() { while(true) { mutexIno.lock(); QList<double> extrudersTemp; for(int i = 0; i < printer->getNoOfExtruders();i++) { extrudersTemp.append(printer->getExtruderTemp(i)); } mutexIno.unlock(); usleep(100000); emit updateTemp(extrudersTemp,printer->getBedTemp()); if(stopLoop==true) break; } } void TemperaturesThread::pausar(bool b) { if(b==true) this->mutexIno.lock(); else this->mutexIno.unlock(); } void TemperaturesThread::setLoop(bool b) { QMutexLocker locker(&mutexIno); this->stopLoop = b; } bool TemperaturesThread::getLoop() { }
-
How is yours mutex is created? Is it a recursive mutex?
-
How is yours mutex is created? Is it a recursive mutex?
@alex_malyu
on my class brprint3d.h
private: QMutex mutexIno;
I need to call a constructor with mutexIno(1) ? -
@alex_malyu
on my class brprint3d.h
private: QMutex mutexIno;
I need to call a constructor with mutexIno(1) ?From 4.8 documentation:
Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex. If this mutex is a non-recursive mutex, this function will dead-lock when the mutex is locked recursively.
This means
QMutexLocker locker(&mutexIno);
mutexIno.lock();was already dead lock
-
From 4.8 documentation:
Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex. If this mutex is a non-recursive mutex, this function will dead-lock when the mutex is locked recursively.
This means
QMutexLocker locker(&mutexIno);
mutexIno.lock();was already dead lock
@alex_malyu but i only calls QMutexLock OR QMutex, not both. I'm just trying to discover which works better.
Because with one or another the program crashs. -
@alex_malyu but i only calls QMutexLock OR QMutex, not both. I'm just trying to discover which works better.
Because with one or another the program crashs.did you find which statement crashes code?
This mutex is created somewhere, are you sure it is not locked from the same tread in the other place? -
and really it should not make much difference beside maybe a bit slower construction in the case of QMutexLocker.
-
I initiaze my QMutex with recursion on my constructor here:
BrPrint3D::BrPrint3D(QWidget *parent) : QMainWindow(parent),mutexIno(QMutex::Recursive),
But dont solve my problem.
The program still crash when i click on the button.
I'm trying to do this way for test:void BrPrint3D::on_bt_table_clicked(bool checked) { //QMutexLocker locker(&mutexIno); if(mutexIno.tryLock(200)) { qDebug() <<"Lock Done"; if(checked==true) { ui->bt_table->setStyleSheet("background-color:red;"); float temp=ui->tb_Table_Temperature->text().toFloat(); printer_object->setBedTemp(temp)==true ? qDebug() <<"OK": qDebug()<<"~OK"; } else { ui->bt_table->setStyleSheet(""); printer_object->setBedTemp(0); } mutexIno.unlock(); } else qDebug()<<"Timeout"; }
the qDebug()<<"Lock Done" appear, but the unlock dont.
This is the first time that i use QThread and QMutex, please help me.
-
@Lays147 said:
You need to find the exact line it crashes at.
Put debug statements at every line like below.
The last statement might tell you where it crashes.First candidate to check is probably printer_object->setBedTemp()
another candidate to check will be places you lock the same mutex,
or access the same objects and forgot to use mutex
You may keep putting printing statements to find it out.Finally try to produce small compilable example which reproduces the problem.
Someone's code always requires time to understand,
but incomplete code often brings nothing but headache, especially when multi-threading is involved.void BrPrint3D::on_bt_table_clicked(bool checked)
{ //QMutexLocker locker(&mutexIno);
if(mutexIno.tryLock(200))
{
qDebug() <<"Lock Done";
if(checked==true)
{
qDebug() <<" after lock 1";
ui->bt_table->setStyleSheet("background-color:red;");
qDebug() <<" after lock 2";
float temp=ui->tb_Table_Temperature->text().toFloat();
qDebug() <<" after lock 3";
printer_object->setBedTemp(temp)==true ? qDebug() <<"OK": qDebug()<<"~OK";
qDebug() <<" after lock 4";} else { qDebug() <<" after lock 5"; ui->bt_table->setStyleSheet(""); qDebug() <<" after lock 6"; printer_object->setBedTemp(0); } qDebug() <<" before unlock"; mutexIno.unlock(); qDebug() <<" after unlock";> } else qDebug()<<"Timeout";
}