Results 1 to 3 of 3

Thread: lock a file

  1. #1
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default lock a file

    Hi,

    While writing on a file from some QThread and another thread try to write on the same file, The code will crash.

    I need to lock the file until my thread finish writing on it. Then unlock it.
    I mean by "lock" : prevent other threads from writing on this file until this thread finish writing on it.

    May you help me.

    Regards,

  2. #2
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: lock a file

    Quote Originally Posted by Malek View Post
    Hi,

    While writing on a file from some QThread and another thread try to write on the same file, The code will crash.

    I need to lock the file until my thread finish writing on it. Then unlock it.
    I mean by "lock" : prevent other threads from writing on this file until this thread finish writing on it.

    May you help me.

    Regards,
    What you are looking for is called a "mutex". I could point to it in the documents but what you really want to do is do a web search and read more about programming in threads both generically and with the specific API provided by Qt.

  3. The following user says thank you to nroberts for this useful post:

    Malek (28th December 2010)

  4. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: lock a file

    If you don't care about other processes being able to write to your file (only other threads in your application), then you should use flock(). Your first thread will lock, write to the file, and then unlock. If another thread attempts during the write, it will block (at the flock() call) until the write is finished and the file is unlocked. Alternatively, you could create a file "filename.lock" which your second thread could detect, but the flock call is preferred as its done in the kernel.

    This would also work if you have multiple copies of your application running, but it will not stop other applications from writing to your file unless they also use flock()

    EDIT: Note that flock does not, typically, work across a networked mount point. I don't know if your file is local or remote. If you require that, I would suggest fctl, but you'll need to check what the server supports first.
    Last edited by squidge; 27th December 2010 at 23:50.

  5. The following user says thank you to squidge for this useful post:

    Malek (28th December 2010)

Similar Threads

  1. Lock a file
    By euthymos in forum Qt Programming
    Replies: 22
    Last Post: 27th December 2010, 23:29
  2. Lock Screen
    By ahmdsd_ostora in forum Qt Programming
    Replies: 16
    Last Post: 11th July 2010, 09:55
  3. Lock strategy
    By pippo42 in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2010, 10:31
  4. Lock a row in QTreeWidget
    By maka3k in forum Qt Programming
    Replies: 1
    Last Post: 9th March 2010, 13:45
  5. QMutex seems not to lock()
    By sylvaticus in forum Qt Programming
    Replies: 18
    Last Post: 4th December 2009, 10:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.