Results 1 to 5 of 5

Thread: Write from multiple QThreads on one file

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

    Default Write from multiple QThreads on one file

    Hi,

    Threads are running simultaneously and writing to the same file. When two threads write to the file in same time, The code will crash.

    Is there any way to organize the writing without using temporary variables?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Write from multiple QThreads on one file

    Learn about mutexes

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write from multiple QThreads on one file

    Create an object that serializes the file access using a QMutex. Your threads can then access a single instance of that object.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    Default Re: Write from multiple QThreads on one file

    Thanks tbscope ,Thanks franz
    may I ask for an example.

    Thanks in advance for your fast response.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Write from multiple QThreads on one file

    You may.

    Note that it will go a lot faster if you search for yourself in the documentation first:
    http://doc.qt.nokia.com/4.7/all-examples.html

    Then you will find things like this:
    http://doc.qt.nokia.com/4.7/threads-waitconditions.

  6. The following user says thank you to tbscope for this useful post:

    Malek (27th December 2010)

Similar Threads

  1. Replies: 7
    Last Post: 13th September 2011, 13:15
  2. Replies: 2
    Last Post: 2nd November 2010, 05:15
  3. multiple QThreads with QUdpSocket and QTcpServer
    By radeberger in forum Qt Programming
    Replies: 0
    Last Post: 25th March 2010, 11:25
  4. QThreads, QMutexes, and multiple access
    By coderbob in forum Qt Programming
    Replies: 3
    Last Post: 12th January 2010, 16:39
  5. about qt debugger and file write
    By guchangyuan in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2009, 02:58

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.