PDA

View Full Version : [SOLVED] QThread - Writting into a file



MonsterPipe
March 15th, 2014, 04:05 AM
Hi,

I have a small QT application that needs to serialize some data periodically. I'm using a QThread with QMutex to protect my resources.

Here is the code I use inside my QThread. I don't know why but it writes no data at all in the file.



void CDataLogger::run(){ static GeneralInformations toto;
QDataStream out(m_File); // we will serialize the data into the file
while(MustRun) { m_Mutex->lock();
out << 45; out << 45; out << 45; out << 45; out << 45; out << 45; out << 45; out << 45;
m_Mutex->unlock(); this->sleep(1); }}

The m_File object is a QFile object pointer receive by the Main Thread. If I use the same code in the main thread, all data is written in the file but if I use it inside another thread, the file stays empty. Is it because the new thread needs some kind of privileges?
I need help on this. :)

Thank you in advance,

MonsterPipe
March 15th, 2014, 04:07 AM
Sorry my code was not correctly formatted

Here it is again:



void CDataLogger::run(){ static GeneralInformations toto;
QDataStream out(m_File); // we will serialize the data into the file
while(MustRun) { m_Mutex->lock();
out << 45; out << 45; out << 45; out << 45; out << 45; out << 45; out << 45; out << 45;
m_Mutex->unlock(); this->sleep(1); }}

MonsterPipe
March 15th, 2014, 04:07 AM
Sorry, I don't know why the code is all messed up.

MonsterPipe
March 15th, 2014, 05:43 AM
Ok, I found my error.

My mutex was not properly initialzed.

Stupid error. :)