Results 1 to 7 of 7

Thread: Thread safety

  1. #1
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Thread safety

    Dear experts,

    I'm working in Qt4.7.4 in ubuntu. Does emitting the signal inside a thread is thread safety?

    Ex:

    Qt Code:
    1. class myThread::public QThread
    2. {
    3. .....
    4. private:
    5. void sendStatus(int num);
    6. Signals:
    7. void mySignal1();
    8. void mySignal2();
    9. };
    10.  
    11.  
    12. void myThread::sendStatus(int num)
    13. {
    14. if(num)
    15. {
    16. emit mySignal1();
    17. }
    18. else
    19. {
    20. emit mySignal2();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. class myClass::public QWidget
    2. {
    3. public:
    4. ........
    5. ........
    6. myThread *m;
    7. void myTrans1();
    8. void myTrans2();
    9.  
    10. };
    11.  
    12. myClass()
    13. {
    14. m=new myThread();
    15. connect(m,SIGNAL(mySignal1()),this,SLOT(myTrans1());
    16. connect(m,SIGNAL(mySignal2()),this,SLOT(myTrans2());
    17. }
    18.  
    19. void myClass::myTrans1()
    20. {
    21. //some code
    22. }
    23.  
    24. void myClass::myTrans2()
    25. {
    26. //some code
    27. }
    To copy to clipboard, switch view to plain text mode 

    is this thread safety?

    Thanks,
    Bala
    Last edited by BalaQT; 25th July 2011 at 13:22.

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Thread safety

    No, if you do this like this!
    QThread class is used to manipulate thread so if you add some functionality there not related to managing thread then you will accouter problems with thread safety.
    See this article: You’re doing it wrong…
    Short: create QObject which should do something in thread, move it to thread, invoke slot which should do something, and then signals will be emitted from this slot in thread safe meaner.
    See also QObject::moveToThread.

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

    BalaQT (26th July 2011)

  4. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Thread safety

    No wonder that people are "doing it wrong", thats what the docs says...

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

    BalaQT (26th July 2011)

  6. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Thread safety

    Quote Originally Posted by You’re doing it wrong…
    I take much of the blame for the confusion that comes with writing threaded Qt code. The original QThread class was abstract, so subclassing was necessary. It wasn’t until Qt 4.4 that QThread::run() gained a default implementation.

    I see in documentation inheritance example, but note that it doesn't provide extra signal slots for QThread, it just creates some QObject in run method nothing else (this leads to a same thing as moveToThread). Anyway I agree that documentation needs update.

  7. The following user says thank you to MarekR22 for this useful post:

    BalaQT (26th July 2011)

  8. #5
    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: Thread safety

    To add my five cents: it is thread-safe per se but it is probably not what you want to achieve.
    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.


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

    BalaQT (26th July 2011)

  10. #6
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Thread safety

    So the right way is, as MarekR22 said,

    1) create QObject which should do something
    2) create a thread object
    3) move the object to created thread
    4) invoke slot which should do something
    5) signals will be emitted from this slot

    Do not inherit the QThread class unless if we need to add/override some features in it.

    Pls correct me , if Im wrong.


    @wysota:
    To add my five cents: it is thread-safe per se but it is probably not what you want to achieve.
    Master , you are saying that the orginal implementation is thread safe. But its not the proper way to do it. right?

    Thanks for the excellent responses,
    Cheers,
    Bala

  11. #7
    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: Thread safety

    Yes, it is thread-safe because signal-slot mechanism assures that. But the slot executes in a different thread than you think so if it contains code that manipulates some data from a different thread, this manipulation will not be thread-safe unless the data itself assures such safety.
    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. Thread-safety of QGraphicsView::items.
    By Nicuvëo in forum Qt Programming
    Replies: 2
    Last Post: 22nd February 2011, 12:24
  2. QWidget and thread safety
    By spraff in forum Qt Programming
    Replies: 3
    Last Post: 22nd January 2009, 01:11
  3. QPointer and thread safety
    By Nb2Qt in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2008, 09:22
  4. QTableWidget Design & Thread Safety Question
    By bpetty in forum Qt Programming
    Replies: 4
    Last Post: 28th March 2008, 00:09
  5. QFile and thread-safety
    By Raistlin in forum Qt Programming
    Replies: 2
    Last Post: 31st January 2008, 15:42

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.