Results 1 to 6 of 6

Thread: Multiprocess access to QSetting files

  1. #1
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Multiprocess access to QSetting files

    If there are multiple processes writing to the same settings file at once (maybe multiple instances of a program are exiting simultaneously due to a system shutdown or something), does Qt internally do some kind of locking to make sure there is no corruption of the file?

    If so, is there any risk of a the lock being left behind in case the process dies in the middle of writing the file?

    If not, is there any recommended way to do file locking within Qt? I tried looking for a class which would do this, but without success. But I easily could have missed it, there are so many classes to learn.

    Thanks!
    - Lana

  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: Multiprocess access to QSetting files

    From the documentation:

    Accessing Settings from Multiple Threads or Processes Simultaneously
    QSettings is reentrant. This means that you can use distinct QSettings object in different threads simultaneously. This guarantee stands even when the QSettings objects refer to the same files on disk (or to the same entries in the system registry). If a setting is modified through one QSettings object, the change will immediately be visible in any other QSettings objects that operate on the same location and that live in the same process.

    QSettings can safely be used from different processes (which can be different instances of your application running at the same time or different applications altogether) to read and write to the same system locations. It uses advisory file locking and a smart merging algorithm to ensure data integrity. Changes performed by another process aren't visible in the current process until sync() is called.

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

    lana (7th September 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Multiprocess access to QSetting files

    Ah, thank-you tbscope! I missed that section in the docs. (embarrassed grin goes here.)
    - Lana

  5. #4
    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: Multiprocess access to QSetting files

    To make this clear:
    QSettings is NOT thread safe if you use only one object accross threads!

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiprocess access to QSetting files

    Quote Originally Posted by tbscope View Post
    To make this clear:
    QSettings is NOT thread safe if you use only one object accross threads!
    After all, it's based on QObject
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Jun 2014
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Maemo/MeeGo

    Default Re: Multiprocess access to QSetting files

    Sorry for using ancient thread, but it seems to be more suitable.

    I have two processes using same QSettings, first one is dbus service, second is gui. server reloading settings values at start and after receiving reload signal from client. client reloading settings at start only and sending reload signal to server process after changing settings in gui.

    Sometimes when shutdown killing processes my qsettings config file erased completely (i have no any settings clear function implemented in my code, so it can't be any sort of coding error). Is there any way to make it work safer? Or it's better to switch from qsettings to dconf because of multiple process usage?

Similar Threads

  1. access the network to read files
    By jaca in forum Newbie
    Replies: 3
    Last Post: 2nd March 2010, 13:00
  2. how to use ms access files
    By meozeren in forum Newbie
    Replies: 1
    Last Post: 30th July 2009, 16:35
  3. Access UI Components from different source files
    By cevou in forum Qt Programming
    Replies: 3
    Last Post: 13th February 2009, 23:44
  4. Access violation when reading big text files
    By robertson1 in forum General Programming
    Replies: 0
    Last Post: 18th September 2008, 06:59
  5. How to access files from specific folder using QContentSet
    By hrudhay in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 30th June 2008, 19:57

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.