Results 1 to 3 of 3

Thread: QThread blocking wait()

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QThread blocking wait()

    Hey there,

    I've implemented a QThread.

    Here is my run function:

    Qt Code:
    1. void qkReceptor::run()
    2. {
    3. // A timer to avoid high CPU charge
    4. QTimer timer;
    5.  
    6. connect(&timer, SIGNAL(timeout()), this, SLOT(onCheckStdin()), Qt::DirectConnection);
    7. connect(this, SIGNAL(newLine(QString)), this, SLOT(onNewLine(QString)), Qt::BlockingQueuedConnection);
    8.  
    9. timer.start(100);
    10.  
    11. QThread::exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Here is my stop function:

    Qt Code:
    1. void qkReceptor::stop()
    2. {
    3. QThread::quit();
    4. QThread::wait(); // Infinite loop here
    5. }
    To copy to clipboard, switch view to plain text mode 

    When calling stop from my main thread, wait blocks everything.
    Why?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread blocking wait()

    The correct way is

    Qt Code:
    1. void qkReceptor::stop()
    2. {
    3. this->quit();
    4. this->wait(); // Infinite loop here
    5. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    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: QThread blocking wait()

    I'd like to note that wait() should be called not from the thread itself but from a thread that created it (which I guess is what you are trying to do or might be trying to do). The semantics for this call is "block me here until the thread in question has finished its execution". If you call it from the worker thread itself then the thread will be suspended and will never finish thus wait() will never end and you'll have a deadlock.
    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.


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

    dpatel (20th April 2011)

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. processEvents() for a QThread
    By hb in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2011, 15:46
  3. Non blocking Gui Qthread in qt3
    By triperzonak in forum Newbie
    Replies: 2
    Last Post: 13th September 2008, 14:06
  4. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51

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.