Results 1 to 6 of 6

Thread: Question about atomic operation and Qt's data sharing.

  1. #1
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Question about atomic operation and Qt's data sharing.

    Lets say I have this sample code:
    Qt Code:
    1. class MyThead : public QThread
    2. {
    3. public:
    4. MyThead() {};
    5. ~MyThread {};
    6.  
    7. QString getTheString() const { return m_someString; }
    8.  
    9. protected:
    10. void run()
    11. {
    12. // Do stuff with m_someString....
    13. }
    14.  
    15. private:
    16. QString m_someString;
    17. }
    To copy to clipboard, switch view to plain text mode 

    If for example I call getTheString() method using other thread context will I get crash or something nasty if I do not synchronize m_someString with mutex? I had read about the reentrant and atomic reference counter in Qt but I'm still not sure if I get it right.

    On my project I have created an implicitly shared class using the QSharedData technique and I have lots of members that are mixed primitive types and Qt types. The big question here is can I share copy instances of my class without the need of mutex or other type locking?
    Last edited by The Storm; 22nd August 2010 at 21:25.

  2. #2
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about atomic operation and Qt's data sharing.

    Quote Originally Posted by The Storm View Post
    The big question here is can I share copy instances of my class without the need of mutex or other type locking?
    Atomic operation are useful when implement a implicitly shared classes.
    When you need share copy instances of any class between difernet threads you need use a mutex or similar classes. to sincronize the access to instances

  3. #3
    Join Date
    Dec 2007
    Posts
    27
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about atomic operation and Qt's data sharing.

    Calling getTheString() would not lead into undefined behavior.

    The Qt documentations says the following:

    Qt uses an optimization called implicit sharing for many of its value class, notably QImage and QString. Beginning with Qt 4, implicit shared classes can safely be copied across threads, like any other value classes. They are fully reentrant. The implicit sharing is really implicit.

    In many people's minds, implicit sharing and multithreading are incompatible concepts, because of the way the reference counting is typically done. Qt, however, uses atomic reference counting to ensure the integrity of the shared data, avoiding potential corruption of the reference counter.
    Greetz Yakin

  4. The following 2 users say thank you to yakin for this useful post:

    ecanela (22nd August 2010), The Storm (22nd August 2010)

  5. #4
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about atomic operation and Qt's data sharing.

    Oh, how I missed that... Thanks a lot.

  6. #5
    Join Date
    Dec 2007
    Posts
    27
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about atomic operation and Qt's data sharing.

    You are welcome The Qt docs are so wide and that part was really hidden.

  7. #6
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about atomic operation and Qt's data sharing.

    Do note that if you manipulate m_someString in the run thread in several places, you can get undefined behavior: The contents of the string may be odd. The best way to tackle this in my view is to
    Qt Code:
    1. void ThreadThingy:run()
    2. {
    3. QString theString = m_someString;
    4. // perform operations on theString
    5. m_someString = theString;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. urgent!!!!!......sharing data
    By alphajoseph in forum Newbie
    Replies: 9
    Last Post: 16th September 2010, 11:42
  2. QVector::data() and implicit sharing
    By abernat in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2009, 18:34
  3. Sharing data between objects
    By Mr_Grieves in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2009, 00:13
  4. Sharing data between threads
    By bbui210 in forum Qt Programming
    Replies: 15
    Last Post: 19th October 2008, 17:56
  5. Sharing data across threads
    By jphn_crichton in forum Qt Programming
    Replies: 11
    Last Post: 5th May 2008, 18:29

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.