Results 1 to 15 of 15

Thread: Workload in a QThread blocks main application's event loop ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Workload in a QThread blocks main application's event loop ?

    Quote Originally Posted by jacek
    That's the run() method, but how do you start that thread?
    I start it from gui thread with
    Qt Code:
    1. otherThread = new OtherThread(this);
    2. otherThread->start();
    To copy to clipboard, switch view to plain text mode 

    OtherThread's declaration:
    Qt Code:
    1. class OtherThread : public QThread
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Workload in a QThread blocks main application's event loop ?

    Quote Originally Posted by 0xBulbizarre
    otherThread = new OtherThread(this);
    otherThread->start();
    Looks OK, did you try with 0 instead of "this"?

  3. #3
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Workload in a QThread blocks main application's event loop ?

    Quote Originally Posted by jacek
    Looks OK, did you try with 0 instead of "this"?
    yes, but nothing changes (apparently).


    I think I found something... The slot executed by the singleShot timer shall be executed by the worker thread, but is in fact executed by the GUI thread. So, I deduce that the connection created by the singleShot timer is a DirectConnection instead of a QueuedConnection. That would explain why the slot is executed by the emitting thread.

    Here is how I create the worker thread, and how I create the singleShot() timer
    Qt Code:
    1. // code from the GUI thread
    2. otherThread = new OtherThread(this);
    3. otherThread->start();
    4. QTimer::singleShot(0, otherThread, SLOT(startThread(void)));
    5. cout << "In GUI Thread, Thread=" << currentThread() << " ThreadID=" << currentThreadId() << endl;
    6. QCoreApplication::processEvents(); // process the timer event
    To copy to clipboard, switch view to plain text mode 

    here is my startThread() function (from the worker thread)
    Qt Code:
    1. void OtherThread::startThread(void) {
    2. cout << "In startThread() Thread=" << currentThread() << " ThreadID=" << currentThreadId() << endl;
    3. // do some job...
    4. }
    To copy to clipboard, switch view to plain text mode 

    outputs where we can see that the GUI thread executes the slot instead of the worker thread:
    In GUI Thread, Thread=0x804f198 ThreadID=3078543040
    In startThread() Thread=0x804f198 ThreadID=3078543040

    Where is my mistake ? should QTimer::singleShot()'s receiver be something else that my otherThread ?

  4. #4
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Workload in a QThread blocks main application's event loop ?

    I saw in the docs that:
    By default, QObject::connect() establishes a direct connection if the sender and the receiver live in the same thread, and a queued connection if they live in different threads.
    If we look at my code above (reproduced below)... we can see that both the sender and the receiver live in the same thread. The sender is the singleShot timer, the receiver is a thread object, also living in the GUI thread.
    Qt Code:
    1. otherThread = new OtherThread(this);
    2. otherThread->start();
    3. QTimer::singleShot(0, otherThread, SLOT(startThread(void)));
    To copy to clipboard, switch view to plain text mode 

    is it correct ? how to solve that problem ?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Workload in a QThread blocks main application's event loop ?

    QThread object lives in the thread that created it. You could split that class into two --- the thread itself and an object that lives within that thread.

  6. The following user says thank you to jacek for this useful post:

    0xBulbizarre (20th March 2006)

  7. #6
    Join Date
    Feb 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Workload in a QThread blocks main application's event loop ?

    Ok, I now understand why a QThread should'nt do any business logic directly. I'll try that tonight!

    thanks for what I learned in this (forum) Thread !

  8. #7
    Join Date
    Apr 2006
    Posts
    7
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Workload in a QThread blocks main application's event loop ?

    Try this

    otherThread = new OtherThread(this);
    otherThread->moveToThread(otherThread);
    otherThread->start();

    that should work ... Regards Madrich

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.