Results 1 to 2 of 2

Thread: QList and implicit sharing question

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

    Default QList and implicit sharing question

    Hello world!

    I am (still) trying to master the implicit sharing feature of Qt containers and I (still) stumble over an issue occasionally. I am attempting to use a QList across two threads and run into a thread safety issue that is triggered by the QList detaching when it doesn't really need to. Here is a sketch of my setup.

    Qt Code:
    1. // I have a global state visible everywhere, a blackboard if you will, with a QList inside.
    2. class GlobState
    3. {
    4. QList<ActionObject> list;
    5. }
    6.  
    7. extern GlobState globState;
    8.  
    9. // This thread is the main control loop that "does" stuff.
    10. class WorkThread : QThread
    11. {
    12. GlobState oldState;
    13.  
    14. // The QList is populated in an init() function at the end of construction time and it is never changed again.
    15. void init()
    16. {
    17. globState.list << this;
    18. globState.list << that;
    19. globState.list << and more;
    20. }
    21.  
    22. // The step() function is called periodically by a timer.
    23. void step()
    24. {
    25. // Here I have to call a non-const method of one of the objects inside the QList.
    26. // I believe this is where the detachment happens.
    27. globState.list[0].doSomething();
    28.  
    29. // Here I take a copy of the global object. This should increase the reference counter
    30. // of the QList, and that is why the doSomething() above should result in a detach.
    31. oldState = globState;
    32. }
    33. }
    34.  
    35. // A gui thread is drawing the contents of the QList.
    36. class GuiThread : QThread
    37. {
    38. // Drawing is also periodic, in a separate thread.
    39. // All access to the global list in this thread is const.
    40. void draw()
    41. {
    42. for (int i = 0; i < globState.list.size(); i++) // .size() is const
    43. {
    44. // This is the line where it crashes:
    45. globState.list.at(i).draw(); // .at() is const, .draw() is const
    46. }
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

    In words, I have a global QList that is accessed by two threads at the same time. The QList is populated before the threads start reading it. The items in the QList are never changed in terms of the number or the position of the items. However, in line 27 in my sketch above, I have to access one of the items in a non-const way and modify the state of this item inside the list. The change is thread safe. Then, I take a copy of the list which should increase the reference counter, which then causes detach()-ment at the next non-const access (Is this correct?). And since the detachment happens sometimes during the time the list is being drawn on the gui by another thread, my program crashes.

    So what can I do in this case? I would really like to avoid using a mutex, because there is no good reason to do so. If the QList would not detach, everything should work fine.

    Cruz

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList and implicit sharing question

    I think you could use a list of ActionObject* and at() instead of the index operator.
    Or use a std::list that copies when copied.

    Cheers,
    _

Similar Threads

  1. Implicit sharing vs. c++ refereces
    By IrYoKu in forum Qt Programming
    Replies: 12
    Last Post: 10th November 2011, 00:20
  2. Classes with Implicit Sharing
    By weaver4 in forum Newbie
    Replies: 5
    Last Post: 15th January 2011, 19:57
  3. Threads and Implicit Sharing
    By stillwaiting in forum Newbie
    Replies: 5
    Last Post: 30th November 2010, 17:46
  4. Question about implicit sharing
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2009, 18:03
  5. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.