Solved How to free file descriptor of same file get accessed by two different thread simultaneously ?
-
I am reading data from 2 slave device in one thread from same i2c bus /dev/i2c-2. And from 2nd thread i am reading data from 3rd salve on same i2c bus.
Simultaneously running of threads occur for 20 second. and then after this error occur " ERROR NO 24: Failed to write to bus Too many open files"
what is right thing i need to do to remove error ERROR NO 24.
My code written below:
#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; QThread::msleep(1000); } } Dialog::~Dialog() { delete ui; }
-
@Qt-embedded-developer said in How to free file descriptor of same file get accessed by two different thread simultaneously ?:
FuelCheck ObjFuelCheck;
ObjFuelCheck.gauge_init();
Why do you instantiate and init ObjFuelCheck inside the loop?!
Same for all the other stuff created inside the loop (like openning the file).
Do it once before you enter the loop...
Also, you really should not write into same file from two threads! This will endup in a mess. If you want to do so for whatever reason, open the file only once and synchronise the access to it (use a QMutexLocker). -
@Qt-embedded-developer said in How to free file descriptor of same file get accessed by two different thread simultaneously ?:
FuelCheck ObjFuelCheck;
ObjFuelCheck.gauge_init();
Why do you instantiate and init ObjFuelCheck inside the loop?!
Same for all the other stuff created inside the loop (like openning the file).
Do it once before you enter the loop...
Also, you really should not write into same file from two threads! This will endup in a mess. If you want to do so for whatever reason, open the file only once and synchronise the access to it (use a QMutexLocker).