why my first thread not run and 2nd thread run continuously ?
-
@Qt-embedded-developer Let's go one step back, you asked: "So what i need to change in above code to cleanly stop thread ?". I pointed you to https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested - did you try it? If so, what is the problem now?
@jsulm i have tried it but still i got same output. i have commented blocking function call it works. but continuous running thread stop /dev/i2c-2 to get open.
this is the main problem. and 2nd problem without comment my blocking function how to make this code get work simultaneously
-
@jsulm i have tried it but still i got same output. i have commented blocking function call it works. but continuous running thread stop /dev/i2c-2 to get open.
this is the main problem. and 2nd problem without comment my blocking function how to make this code get work simultaneously
@Qt-embedded-developer Please show the code.
Also, I asked before for how long the blocking function is actually blocking? -
@Qt-embedded-developer Please show the code.
Also, I asked before for how long the blocking function is actually blocking?thread 1 execute for only one time then it get block. while thread 2 run continuously
#include "dialog.h" #include "ui_dialog.h" #include <QDateTime> #include <QDebug> #include <QFile> #include <QTextStream> Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) { ui->setupUi(this); StartThread(); qDebug() << "End of Program"; } void Dialog:: StartThread() { HelloThread_1 thread1; HelloThread_2 thread2; thread1.start(); qDebug() << "hello from GUI thread " << thread1.currentThreadId(); thread2.start(); qDebug() << "hello from GUI thread " << thread2.currentThreadId(); thread1.wait(); // do not exit before the thread is completed! thread2.wait(); // do not exit before the thread is completed! } void HelloThread_1::run() { while(1) { if(QThread::currentThread()->isInterruptionRequested()) return; qDebug() << "hello from worker thread AAAA"; FuelCheck ObjFuelCheck; ObjFuelCheck.gauge_init(); int Percentage = -1; bool bisFuelConnected = true; ObjFuelCheck.gauge_cmd_read(ObjFuelCheck.nI2CId, 0x2c); qDebug() << __LINE__; Percentage = ObjFuelCheck.u16Datareceived; qDebug()<< "percentage :"<<Percentage; QFile data("/home/user/output.txt"); if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) { QTextStream out(&data); out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "percentage :"<<Percentage<<"\n"; // writes "Result: 3.14 2.7 " } QThread::msleep(100); if((Percentage < 0) && (Percentage > 100)) { bisFuelConnected = false; qDebug() << "Fuel gauge found but wrong percentarge detected : " + QString::number(Percentage); } **// below part block function ** FuelGaugeProfileUpdate *pFuelGaugeProfileUpdate = new FuelGaugeProfileUpdate(); if(IS_VALID_OBJ(pFuelGaugeProfileUpdate)) { pFuelGaugeProfileUpdate->bIsRequiredToUpdateFuelGaugeProfile =true; if(pFuelGaugeProfileUpdate->bIsRequiredToUpdateFuelGaugeProfile) { pFuelGaugeProfileUpdate->show(); } else { DELETE_OBJ(pFuelGaugeProfileUpdate); } } //------------------------------------------------ NfcTagReaderWriter *objNFCTag_write = new NfcTagReaderWriter(); if(IS_VALID_OBJ(objNFCTag_write)) { if(objNFCTag_write->NFCInit()) { if(objNFCTag_write->ReadNdefTextMessage() == 0x00) { qDebug()<<"WriteNdefTextMessage"; QFile data("/home/user/output.txt"); if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) { QTextStream out(&data); out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "objNFCTag_write->Text.text :"<<objNFCTag_write->Text.text<<"\n"; // writes "Result: 3.14 2.7 " } //objNFCTag_write->WriteNdefTextMessage(); } } qDebug() << __LINE__; DELETE_OBJ(objNFCTag_write); } qDebug() << __LINE__; QThread::msleep(1000); } } void HelloThread_2::run() { while(1) { if(QThread::currentThread()->isInterruptionRequested()) return; qDebug() << "hello from worker thread BBBB"; ExtAdc ObjExtAdc; AdcSettings AdcInfo; uint8_t u8ReadADC_no = 2; ObjExtAdc.AdcReadFile((ADC_type)(u8ReadADC_no)); QString sADC_value = QString::number(ObjExtAdc.i32AdcReadValue); QFile data("/home/user/output.txt"); if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) { QTextStream out(&data); out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "u16AdcValue "<<sADC_value<<"\n"; // writes "Result: 3.14 2.7 " } qDebug()<<"u16AdcValue "<<sADC_value; /* uint16_t u16AdcValue = 0; bool bIsVinVolt = false; float fVinVolt = 0.0; ObjExtAdc.GetAvgValue(ADC_BATTERYLEVEL); if(AdcInfo.bAdcReadError == true) { qDebug()<<"adc read error"; } else { u16AdcValue = AdcInfo.u16AdcAverageValue[ADC_BATTERYLEVEL]; QFile data("/home/user/output.txt"); if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) { QTextStream out(&data); out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "u16AdcValue "<<u16AdcValue<<"\n"; // writes "Result: 3.14 2.7 " } qDebug()<<"u16AdcValue "<<u16AdcValue; if(u16AdcValue < 11500) { bIsVinVolt =false; //TableInsertData(sBattery(), sLow()); // + " " + QString::number(u16AdcValue)); } else { bIsVinVolt =true; //TableInsertData(sBattery() , sOk()); // + QString::number(u16AdcValue)); } } */ QThread::msleep(1000); } } Dialog::~Dialog() { delete ui; } -
thread 1 execute for only one time then it get block. while thread 2 run continuously
#include "dialog.h" #include "ui_dialog.h" #include <QDateTime> #include <QDebug> #include <QFile> #include <QTextStream> Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) { ui->setupUi(this); StartThread(); qDebug() << "End of Program"; } void Dialog:: StartThread() { HelloThread_1 thread1; HelloThread_2 thread2; thread1.start(); qDebug() << "hello from GUI thread " << thread1.currentThreadId(); thread2.start(); qDebug() << "hello from GUI thread " << thread2.currentThreadId(); thread1.wait(); // do not exit before the thread is completed! thread2.wait(); // do not exit before the thread is completed! } void HelloThread_1::run() { while(1) { if(QThread::currentThread()->isInterruptionRequested()) return; qDebug() << "hello from worker thread AAAA"; FuelCheck ObjFuelCheck; ObjFuelCheck.gauge_init(); int Percentage = -1; bool bisFuelConnected = true; ObjFuelCheck.gauge_cmd_read(ObjFuelCheck.nI2CId, 0x2c); qDebug() << __LINE__; Percentage = ObjFuelCheck.u16Datareceived; qDebug()<< "percentage :"<<Percentage; QFile data("/home/user/output.txt"); if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) { QTextStream out(&data); out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "percentage :"<<Percentage<<"\n"; // writes "Result: 3.14 2.7 " } QThread::msleep(100); if((Percentage < 0) && (Percentage > 100)) { bisFuelConnected = false; qDebug() << "Fuel gauge found but wrong percentarge detected : " + QString::number(Percentage); } **// below part block function ** FuelGaugeProfileUpdate *pFuelGaugeProfileUpdate = new FuelGaugeProfileUpdate(); if(IS_VALID_OBJ(pFuelGaugeProfileUpdate)) { pFuelGaugeProfileUpdate->bIsRequiredToUpdateFuelGaugeProfile =true; if(pFuelGaugeProfileUpdate->bIsRequiredToUpdateFuelGaugeProfile) { pFuelGaugeProfileUpdate->show(); } else { DELETE_OBJ(pFuelGaugeProfileUpdate); } } //------------------------------------------------ NfcTagReaderWriter *objNFCTag_write = new NfcTagReaderWriter(); if(IS_VALID_OBJ(objNFCTag_write)) { if(objNFCTag_write->NFCInit()) { if(objNFCTag_write->ReadNdefTextMessage() == 0x00) { qDebug()<<"WriteNdefTextMessage"; QFile data("/home/user/output.txt"); if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) { QTextStream out(&data); out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "objNFCTag_write->Text.text :"<<objNFCTag_write->Text.text<<"\n"; // writes "Result: 3.14 2.7 " } //objNFCTag_write->WriteNdefTextMessage(); } } qDebug() << __LINE__; DELETE_OBJ(objNFCTag_write); } qDebug() << __LINE__; QThread::msleep(1000); } } void HelloThread_2::run() { while(1) { if(QThread::currentThread()->isInterruptionRequested()) return; qDebug() << "hello from worker thread BBBB"; ExtAdc ObjExtAdc; AdcSettings AdcInfo; uint8_t u8ReadADC_no = 2; ObjExtAdc.AdcReadFile((ADC_type)(u8ReadADC_no)); QString sADC_value = QString::number(ObjExtAdc.i32AdcReadValue); QFile data("/home/user/output.txt"); if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) { QTextStream out(&data); out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "u16AdcValue "<<sADC_value<<"\n"; // writes "Result: 3.14 2.7 " } qDebug()<<"u16AdcValue "<<sADC_value; /* uint16_t u16AdcValue = 0; bool bIsVinVolt = false; float fVinVolt = 0.0; ObjExtAdc.GetAvgValue(ADC_BATTERYLEVEL); if(AdcInfo.bAdcReadError == true) { qDebug()<<"adc read error"; } else { u16AdcValue = AdcInfo.u16AdcAverageValue[ADC_BATTERYLEVEL]; QFile data("/home/user/output.txt"); if (data.open(QFile::WriteOnly | QFile::Append | QFile::Text)) { QTextStream out(&data); out << "Result: "<< QDateTime::currentDateTime().toString("dd MMM yyyy HH:mm:ss") << " "<< "u16AdcValue "<<u16AdcValue<<"\n"; // writes "Result: 3.14 2.7 " } qDebug()<<"u16AdcValue "<<u16AdcValue; if(u16AdcValue < 11500) { bIsVinVolt =false; //TableInsertData(sBattery(), sLow()); // + " " + QString::number(u16AdcValue)); } else { bIsVinVolt =true; //TableInsertData(sBattery() , sOk()); // + QString::number(u16AdcValue)); } } */ QThread::msleep(1000); } } Dialog::~Dialog() { delete ui; }@Qt-embedded-developer Just for your information all devices connected to same i2c bus
-
@Qt-embedded-developer Just for your information all devices connected to same i2c bus
@Qt-embedded-developer i have found that there i am getting error due to
" Errno 24 : Too many open files" on /dev/i2c-2" -
@Qt-embedded-developer i have found that there i am getting error due to
" Errno 24 : Too many open files" on /dev/i2c-2"@Qt-embedded-developer
Linux errno 24 (EMFILE) is too many open files for your process (or maybe your user, but not I think for the system). I think you can alter this viaulimit, or others. But obviously you need to understand where you are keeping open file handles from. -
@Qt-embedded-developer
Linux errno 24 (EMFILE) is too many open files for your process (or maybe your user, but not I think for the system). I think you can alter this viaulimit, or others. But obviously you need to understand where you are keeping open file handles from. -
@Qt-embedded-developer said in why my first thread not run and 2nd thread run continuously ?:
am i correct ?
No, you are wrong. You just have to make sure you don't open too many files at once.
I ask now for the third (and last) time: for how long does your blocking function block?
-
@Qt-embedded-developer said in why my first thread not run and 2nd thread run continuously ?:
am i correct ?
No, you are wrong. You just have to make sure you don't open too many files at once.
I ask now for the third (and last) time: for how long does your blocking function block?
@jsulm 30 to 50 second and infinite when some error happen while i2c write
-
@jsulm 30 to 50 second and infinite when some error happen while i2c write
@Qt-embedded-developer Then it should work as long as no errors happen with isInterruptionRequested if you use it correctly. But from your code I do not see where you actually request the thread to stop (you just wait for the threads)? If you do not ask it to stop why should it stop?!
If you read https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested you will find link to https://doc.qt.io/qt-5/qthread.html#requestInterruption which you can use to ask the thread to stop.