Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to lock QFile across processes in Qt4.8?
QtWS25 Last Chance

How to lock QFile across processes in Qt4.8?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.2k Views
  • 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 Offline
    M Offline
    menkey18
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      dfaure
      wrote on last edited by
      #2

      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
      0
      • D Offline
        D Offline
        dfaure
        wrote on last edited by
        #3

        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
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved