Real Time Operation - QThread?
-
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";
}