Results 1 to 6 of 6

Thread: Threads and Implicit Sharing

  1. #1
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Threads and Implicit Sharing

    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?

  2. #2
    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: Threads and Implicit Sharing

    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.
    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.


  3. #3
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads and Implicit Sharing

    Quote Originally Posted by wysota View Post
    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/th...plicit-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."

  4. #4
    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: Threads and Implicit Sharing

    Quote Originally Posted by stillwaiting View Post
    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.
    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.


  5. The following user says thank you to wysota for this useful post:

    stillwaiting (30th November 2010)

  6. #5
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads and Implicit Sharing

    Quote Originally Posted by wysota View Post
    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?

  7. #6
    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: Threads and Implicit Sharing

    Quote Originally Posted by stillwaiting View Post
    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.
    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.


Similar Threads

  1. Implicit sharing vs. c++ refereces
    By IrYoKu in forum Qt Programming
    Replies: 12
    Last Post: 9th November 2011, 23:20
  2. Implicit sharing and pointers
    By Luc4 in forum Qt Programming
    Replies: 4
    Last Post: 16th June 2010, 10:31
  3. QVector::data() and implicit sharing
    By abernat in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2009, 18:34
  4. Question about implicit sharing
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2009, 17:03
  5. QSharedData - implicit sharing
    By gyre in forum Newbie
    Replies: 4
    Last Post: 28th October 2007, 19:09

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.