Results 1 to 4 of 4

Thread: QThread - Writting into a file

  1. #1
    Join Date
    Sep 2013
    Beans
    13

    QThread - Writting into a file

    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.

    Code:
    voidCDataLogger::run(){staticGeneralInformationstoto;
    QDataStreamout(m_File);//wewillserializethedataintothefile
    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,

  2. #2
    Join Date
    Sep 2013
    Beans
    13

    Re: QThread - Writting into a file

    Sorry my code was not correctly formatted

    Here it is again:

    Code:
    voidCDataLogger::run(){staticGeneralInformationstoto;
    QDataStreamout(m_File);//wewillserializethedataintothefile
    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);}}

  3. #3
    Join Date
    Sep 2013
    Beans
    13

    Re: QThread - Writting into a file

    Sorry, I don't know why the code is all messed up.

  4. #4
    Join Date
    Sep 2013
    Beans
    13

    Re: QThread - Writting into a file

    Ok, I found my error.

    My mutex was not properly initialzed.

    Stupid error.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •