How to lock QFile across processes in Qt4.8?
-
Hi everyone,
I am working on ubuntu 12.04 64.
And I am looking for a class or a function to lock a QFile across PROCESSES, not threads.There used to be a range of classes performing the similar functionalities, but I can't see them available in Qt4.8:
- QSystemReadWriteLock
- QLock
- QtLockedFile
- QSystemMutex
I wonder where have they gone, and why.
QSystemSemaphore does not work in my case, since the other processes accessing the file is not written in Qt.
I tried to use the C solution, fcntl(fd, F_SETLKW, &lock);
but it always return -1 (error) if the file is opened by QFile. Although it works using the function open() from C libs.
I can understand it used to work, as I found some Q classes using it in the past.Currently I am counting on QxtFileLock to provide the same functionality, but there would be an additional lib in my system and would not be the best thing to do in my case.
but it struggles for me to understand the reason to remove file locking functionalities from Qt.
Or does it exist somewhere that I haven't find so far?Thank you very much and hope to find a nice and neat solution in my working case.
Cheers.
Lan
-
For now you could try using a copy of the QtLockedFile class, from the qt-creator sources.
I'm starting to work on a new implementation for Qt-5.1, based on my work on KLockFile in the past.
The problem with the QtLockedFile implementation, according to my earlier research, is that fcntl(F_SETLK) locks other processes, but not other threads (we might as well handle both...) and it unlocks when just reading the file in the same process (!). http://apenwarr.ca/log/?m=201012#13
So instead I'm setting out to write a QLockFile class, which does NOT inherit from QFile, but creates a separate lock file. -
Feel free to review https://codereview.qt-project.org/46583