Results 1 to 2 of 2

Thread: Where a QThread lives

  1. #1
    Join Date
    Oct 2008
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Where a QThread lives

    Hi

    When I create a Qthread, where does it live? In the thread where it was created or in the new thread created when this object runs?

    When we connect signals to slots of a QThread object where are this slots executed?

    Thanks in advance
    Regards.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Where a QThread lives

    A thread has its own stack, stack pointer, program counter and processor register.
    So it does not live in the main thread (the application address space).

    You do have access to the application address space though through mutexes, semaphores etc...

    Edit: about the signals and slots:
    This is complex, it has to deal with thread affinity.
    A QObject's slots and signals are bound to one single thread.
    If you create a thread from within the main thread, all signals and slots go via the main thread.

    To use signals and slots in the new thread, you need to call moveToThread on the object.
    Example:
    Qt Code:
    1. MyObject myNewObject;
    2. QThread myObjectThread;
    3. myNewObject.moveToThread(&myObjectThread);
    4. myObjectThread.start();
    To copy to clipboard, switch view to plain text mode 

    Do not use moveToThread from within a subclassed thread.
    Do create a new QObject with your own slots and signals and then move it from within the main thread to a new thread.

    Edit again:
    To clarify something:
    The signals and slots of the QThread itself are ALWAYS executed in the main thread.
    Only if you use moveToThread, the signals and slots of the OBJECT that is moved to a new thread (note: this is NOT the QThread object), are executed in the new thread. The QThread object will act as a path between the main thread and the object running in the new thread.

    In short: QThread is NOT a new thread!!!!!!!!!!! In the example above: myObjectThread is NOT a new thread (how confusing this might be) but myNewObject does live in a new thread.
    Last edited by tbscope; 5th July 2010 at 09:34.

Similar Threads

  1. Need help in Qthread
    By skumar434 in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2009, 17:21
  2. Replies: 4
    Last Post: 26th June 2008, 18:41
  3. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54
  4. QThread?
    By vishal.chauhan in forum Newbie
    Replies: 4
    Last Post: 26th March 2007, 13:43
  5. Help with QThread
    By manucorrales in forum Qt Programming
    Replies: 8
    Last Post: 14th October 2006, 20:25

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.