Results 1 to 4 of 4

Thread: changing thread affinity properly

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default changing thread affinity properly

    Hi there. I'd like to know what is the proper way of using moveToThread function. I have QTcpSocket object as a member of QThread derived class. I've got some warning messages about thread affinity. To solve that I call moveToThread inside my thread class:

    Qt Code:
    1. class MyDownloadingThread: public QThread
    2. {
    3. //...
    4. protected:
    5. QTcpSocket socket;
    6. //...
    7. }
    8.  
    9. //....
    10.  
    11. MyDownloadingThread::MyDownloadingThread(ParentThread *parentThread)
    12. {
    13. this->parentThread = parentThread;
    14. this->socket.moveToThread(this);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Is it correct? Or maybe I should call moveToThread after starting the target thread?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: changing thread affinity properly

    if you check in a debugger (or debug output) you will see that your case 'this' and 'parentThread' are the same.
    Your QThread starts a new thread only after start() has been called (which internally means after run() hast been called).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    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: changing thread affinity properly

    Read this article. There is good explanation.
    In general it means that this information is needed for connection (automatic or queued) in which tread slot should be run when connected signal is emitted.
    Note that:
    1. you don't need to subclass QThread
    2. never move thread to itself.

  4. #4
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: changing thread affinity properly

    thanks for the article. now i see it is not necessary to subclass QThread, but, honestly, i don't see much difference. We can write something like
    Qt Code:
    1. QThread *thread = new QThread;
    2. Worker *worker = new Worker;
    3. worker->moveToThread(thread);
    4.  
    5. // now start the thread
    6. thread->start();
    To copy to clipboard, switch view to plain text mode 

    or we can subclass it and move the object in the constructor:
    Qt Code:
    1. MyThread::MyThread()
    2. {
    3. myobject.moveToThread(this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    in both cases object is moved to the thread before it is started, so in both cases the object seems to be moved to the thread interface that QThread is.

    and i don't move thread to itself - i move objects to a thread (see my thread declaration).

Similar Threads

  1. Replies: 5
    Last Post: 21st August 2009, 15:10
  2. Replies: 2
    Last Post: 10th August 2009, 09:45
  3. QProcess processor affinity
    By ibergmark in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2008, 16:09
  4. QThread affinity
    By brcain in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2007, 19:30

Tags for this Thread

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.