PDA

View Full Version : Multiprocess access to QSetting files



lana
7th September 2010, 19:46
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!

tbscope
7th September 2010, 19:57
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.

lana
7th September 2010, 20:49
Ah, thank-you tbscope! I missed that section in the docs. (embarrassed grin goes here.)

tbscope
7th September 2010, 20:51
To make this clear:
QSettings is NOT thread safe if you use only one object accross threads!

wysota
8th September 2010, 00:47
To make this clear:
QSettings is NOT thread safe if you use only one object accross threads!

After all, it's based on QObject :)

coderus
21st June 2014, 08:42
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?