Is QSettings thread safe?
-
Hi there,
I found in our legacy project following codevoid Foo::save() { QSettings settings; if (something) { QReadLocker locker(&lock_); // is valid to use lock? settings->setValue("widgetState", widgetStateValue); ... settings->sync(); } }
Thanks a lot!
-
Hi there,
I found in our legacy project following codevoid Foo::save() { QSettings settings; if (something) { QReadLocker locker(&lock_); // is valid to use lock? settings->setValue("widgetState", widgetStateValue); ... settings->sync(); } }
Thanks a lot!
-
@jeremy_k So, that mean is read/write methods of QSettings are not thread-safe against only registerFormat?
-
@jeremy_k So, that mean is read/write methods of QSettings are not thread-safe against only registerFormat?
@Alexey-Serebryakov said in Is QSettings thread safe?:
@jeremy_k So, that mean is read/write methods of QSettings are not thread-safe against only registerFormat?
Yes, that is what the documentation says.
QSettings is derived from QObject, so in general an instance should only be used from the thread it is associated with.
-
@jeremy_k So, that mean is read/write methods of QSettings are not thread-safe against only registerFormat?
Any particular reason you want to create a global settings object?
... and related question, how do you construct said global object? -
Any particular reason you want to create a global settings object?
... and related question, how do you construct said global object?@kshegunov said in Is QSettings thread safe?:
Any particular reason you want to create a global settings object?
There's a discrepancy between the thread title and the post body. The title indicates a concern about threading. The function in OP shows a function local QSettings object, and no obvious way for it to be used in a different thread.
Using QReadLocker prior to QSettings::setValue() and sync() is curious.
-
@kshegunov said in Is QSettings thread safe?:
Any particular reason you want to create a global settings object?
There's a discrepancy between the thread title and the post body. The title indicates a concern about threading. The function in OP shows a function local QSettings object, and no obvious way for it to be used in a different thread.
Using QReadLocker prior to QSettings::setValue() and sync() is curious.
@jeremy_k said in Is QSettings thread safe?:
There's a discrepancy between the thread title and the post body.
Indeed. I just ignored the local variable as it didn't make sense to me :P