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

Qt-solutions Archive, April 2006
AW: QtLockedFile does not work


Message 1 in thread

Hi

Just a guess as I did not use this
Try to lock the file _before_ open()ing it

Michael 
 
-----Ursprüngliche Nachricht-----
Von: Falko Buttler [mailto:falko.buttler@xxxxxxxx] 
Gesendet: Freitag, 07. April 2006 09:37
An: qt-solutions@xxxxxxxxxxxxx
Betreff: QtLockedFile does not work

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 ] 

Message 2 in thread

Hello Michael,

no you have to use this order. If you first lock and then open you get 
an debug message of QtLockedFile that the file is not opened yet.
But thanks for your help.

Regards,
Falko

Michael.Soukup@xxxxxxxxxx schrieb:
> Hi
>
> Just a guess as I did not use this
> Try to lock the file _before_ open()ing it
>
> Michael 
>  
> -----Ursprüngliche Nachricht-----
> Von: Falko Buttler [mailto:falko.buttler@xxxxxxxx] 
> Gesendet: Freitag, 07. April 2006 09:37
> An: qt-solutions@xxxxxxxxxxxxx
> Betreff: QtLockedFile does not work
>
> 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 ] 

Message 3 in thread

Hm,

strange, but when I first lock and then open as Michael said, everything 
works. But then you get a lot of debug messages of Qt saying   
"QtLockedFile::lock(): file is not opened". Thats annoying and confusing...

Regards,
Falko

Falko Buttler schrieb:
> Hello Michael,
>
> no you have to use this order. If you first lock and then open you get 
> an debug message of QtLockedFile that the file is not opened yet.
> But thanks for your help.
>
> Regards,
> Falko
>
> Michael.Soukup@xxxxxxxxxx schrieb:
>> Hi
>>
>> Just a guess as I did not use this
>> Try to lock the file _before_ open()ing it
>>
>> Michael  
>> -----Ursprüngliche Nachricht-----
>> Von: Falko Buttler [mailto:falko.buttler@xxxxxxxx] Gesendet: Freitag, 
>> 07. April 2006 09:37
>> An: qt-solutions@xxxxxxxxxxxxx
>> Betreff: QtLockedFile does not work
>>
>> 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 ]