Trolltech Home | Qt-solutions Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-solutions Archive, April 2006
QtLockedFile does not work


Message 1 in thread

Hello,

does anybody of you use the QtLockedFile solution for Qt 4?
Under Windows XP it does not work correct until there are parallel 
processes who want to access the file.

Here my small example code:

#include "qtlockedfile.h"

#include <QtDebug>
#include <QString>

#ifdef Q_OS_WIN32
// for getting process id under windows
#include <process.h>
#else
// for getting process id under unix systems
#include <unistd.h>
#endif

int main( int, char** )
{
  qDebug() << "Locking file...";

  QtLockedFile lf("test.log");
  lf.open( QIODevice::WriteOnly|QIODevice::Append );

  lf.lock( QtLockedFile::WriteLock, true );

  qDebug() << "Locked file"; 

  QString message = QString( "[%1] hello world\n" ).arg( getpid() );
  lf.write(message.toLatin1().constData(), 
qstrlen(message.toLatin1().data()));
  lf.flush();

  qDebug() << "Unlocking file...";
  lf.unlock();

  qDebug() << "Unlocked file";
 
  return 0;
}


Here the code to force parallel processes to access the file:

#include <QProcess>

int main( int, char** )
{
  for( int i = 0; i < 10; i++ ) {
    QProcess::startDetached( "lockedFileWriter.exe" );
  }

  return 0;
}


If you do that, there are 10 processes which all freeze and wait for a 
unlocked file .... forever.

What am I doing wrong? I am sure someone at Trolltech has tested this 
and it is a fault on my side but I cannot find one myself.

Regards,
Falko

--
 [ signature omitted ]