Results 1 to 5 of 5

Thread: Question about implicit sharing

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    101
    Thanked 15 Times in 15 Posts

    Default Question about implicit sharing

    I want to make sure I understand this right.

    I have an implicitly shared container, say a QHash. Object A creates it and passes it to object B by value.

    Qt Code:
    1. A
    2. {
    3. B.takeThis(QHash);
    4. }
    To copy to clipboard, switch view to plain text mode 

    So far, when reading from the QHash both A and B would read from the same memory block, right?

    Now A modifies the QHash.

    Qt Code:
    1. A
    2. {
    3. B.takeThis(QHash);
    4. QHash.addElement();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Does the QHash get detached in this case? Or only when B modifies its own copy?

    And if it does get detached, what happens when A modifies the QHash multiple times? Does it get detached every time?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Question about implicit sharing

    Quote Originally Posted by Cruz View Post
    So far, when reading from the QHash both A and B would read from the same memory block, right?
    Yes, exactly. There are multiple instances of QHash, but the internal data is shared between the instances.

    Now A modifies the QHash.

    Does the QHash get detached in this case? Or only when B modifies its own copy?
    Yes, it gets automatically detached when modifying and if the reference count is greater than one.

    And if it does get detached, what happens when A modifies the QHash multiple times? Does it get detached every time?
    No, because after detaching in the first place the reference count is updated appropriately. From then on, it's the only occurrence using the modified data. There is no need to detach if the reference count is one.
    J-P Nurmi

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

    Cruz (17th August 2016)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    101
    Thanked 15 Times in 15 Posts

    Default Re: Question about implicit sharing

    I'm trying to understand the thread safety issue in the following construction:

    Qt Code:
    1. Thread1()
    2. {
    3. QHash data;
    4.  
    5. forever
    6. {
    7. data.modify();
    8. Thread2.takeThis(data);
    9.  
    10. msleep(20);
    11. }
    12. }
    13.  
    14. Thread2()
    15. {
    16. QHash data;
    17.  
    18. takeThis(data)
    19. {
    20. this->data = data;
    21. }
    22.  
    23. forever
    24. {
    25. qDebug() << data;
    26.  
    27. msleep(20);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    If Thread1 happens to modify the data while Thread2 is qDebug()-ing it, then nothing bad happens, because the data is detached, right?

    If above statement is true, then the problem must occur, when takeThis() of Thread2 overwrites it's own data while it's in the process of qDebug()-ing.

    And if both statements are true, then I don't understand why the following construction still crashes:

    Qt Code:
    1. Thread1()
    2. {
    3. QHash data;
    4.  
    5. forever
    6. {
    7. data.modify();
    8. Thread2.takeThis(data);
    9.  
    10. msleep(20);
    11. }
    12. }
    13.  
    14. Thread2()
    15. {
    16. QHash data;
    17. bool reading = false;
    18.  
    19. takeThis(data)
    20. {
    21. if (!reading)
    22. this->data = data;
    23. }
    24.  
    25. forever
    26. {
    27. reading = true;
    28. qDebug() << data;
    29. reading = false;
    30.  
    31. msleep(20);
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    I'm trying to find a solution, where Thread1 is not blocked by a mutex when trying to deliver its data, but rather doesn't deliver and continues to calculate the next data set.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Question about implicit sharing

    J-P Nurmi

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

    Default Re: Question about implicit sharing

    Just note the difference between reentrancy and thread-safety.

Similar Threads

  1. Implicit sharing vs. c++ refereces
    By IrYoKu in forum Qt Programming
    Replies: 12
    Last Post: 10th November 2011, 00:20
  2. QSharedData - implicit sharing
    By gyre in forum Newbie
    Replies: 4
    Last Post: 28th October 2007, 20: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
  •  
Qt is a trademark of The Qt Company.