PDA

View Full Version : Threads and Implicit Sharing



stillwaiting
30th November 2010, 14:09
Dear friends,

I'm a little bit confused with classes that use implicit sharing and with thread-safety issue.

The question I have is: there is a QString and I want to read it from 2 different threads simultaneously. There is guarantee nobody will change QString's content (it is by design of my application). Do I have to protect it with QMutex anyway?

wysota
30th November 2010, 15:41
You can read the contents from two threads without safeguards. But you can't read from one thread and write from another thread without a mutex.

stillwaiting
30th November 2010, 15:55
You can read the contents from two threads without safeguards. But you can't read from one thread and write from another thread without a mutex.

Hi wysota, thank you for your reply. I'm wondering if such behavior is described somewhere in the documentation? The only information I've found about threads and implicit sharing is : http://doc.qt.nokia.com/qtopia4.2/threads.html#threads-and-implicit-sharing , but it says nothing about readonly access from different threads. Just for comparison: this is what the documentation says about the same issue for containers:

"The container classes are implicitly shared, they are reentrant, and they are optimized for speed, low memory consumption, and minimal inline code expansion, resulting in smaller executables. In addition, they are thread-safe in situations where they are used as read-only containers by all threads used to access them."

wysota
30th November 2010, 16:23
Hi wysota, thank you for your reply. I'm wondering if such behavior is described somewhere in the documentation
No, it is purely based on knowledge how this is implemented and knowledge what access from different threads can break in an object (any object). Using const methods of an object (any object) from different threads is always safe unless some members of a class are declared as mutable. This is also the case for implicitly shared classes. The only doubtful situation is when an implicitly shared object is copied but since this only happens when an object is modified (hence not from within a const method), the object is safe for read-only access from different threads. Note, you have to make sure no member of the class (or its private implementation) is mutable. Should an internal representation of the class be changed to introduce a mutable non-atomic member, your existing code might stop working.

stillwaiting
30th November 2010, 17:23
Should an internal representation of the class be changed to introduce a mutable non-atomic member, your existing code might stop working.

But... how can I be sure that mutable parameters will not appear in QString's (or others' implicitly shared Qt4 classes) implementation in future versions of Qt?

wysota
30th November 2010, 17:46
how can I be sure that mutable parameters will not appear in QString's (or others' implicitly shared Qt4 classes) implementation in future versions of Qt?
You can't be sure but in general if they appeared, it would break the implicit sharing functionality as well so it's highly unlikely to happen.
You also can't guarantee some developer that uses your code will not try to cast away constness from an item and call non-const methods on the const object. But all this is not related to implcitly-shared classes, it all applies to any reentrant class. Implicit sharing itself does not impose any limitations regarding this issue.