Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to lock QFile across processes in Qt4.8?

    General and Desktop
    2
    3
    4863
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      menkey18 last edited by

      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

      1 Reply Last reply Reply Quote 0
      • D
        dfaure last edited by

        For now you could try using a copy of the QtLockedFile class, from the qt-creator sources.

        http://qt.gitorious.net/qt-creator/qt-creator/trees/f61e8672db50445a3f84d37e657d38fbaa0d5083/shared/qtlockedfile

        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.

        David Faure (david.faure@kdab.com)
        KDE/Qt Senior Software Engineer
        KDAB - Qt Experts - Platform-independent software solutions

        1 Reply Last reply Reply Quote 0
        • D
          dfaure last edited by

          Feel free to review https://codereview.qt-project.org/46583

          David Faure (david.faure@kdab.com)
          KDE/Qt Senior Software Engineer
          KDAB - Qt Experts - Platform-independent software solutions

          1 Reply Last reply Reply Quote 0
          • First post
            Last post