Results 1 to 4 of 4

Thread: Calling QThread.wait() from mainthread

  1. #1
    Join Date
    Jun 2011
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Calling QThread.wait() from mainthread

    Hello everyone,

    I tried code similar to the following (i shortened it a bit):
    Qt Code:
    1. class WorkerThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. ~WorkerThread();
    6. protected:
    7. void run();
    8. private slots:
    9. void update();
    10. private:
    11. volatile bool m_isInterrupted;
    12. };
    13. WorkerThread::~Workerthread()
    14. {
    15. m_isInterrupted = true;
    16. wait();
    17. }
    18. void WorkerThread::run()
    19. {
    20. QTimer * timer = new QTimer(this);
    21. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    22. timer->start(80);
    23. exec();
    24. }
    25. void WorkerThread::update()
    26. {
    27. if(m_isInterrupted)
    28. {
    29. quit();
    30. }
    31. //do something
    32. }
    To copy to clipboard, switch view to plain text mode 

    The wait() call is needed to prevent the main thread from deleting the workerThread object, while it is still running...

    Note: I tested it with slightly different code, so I'm not 100% sure if the following statements are accurate, but I think so:
    When closing the application, the process does not terminate and the quit() method is never called, which I verified by debugging.

    I think this happens because when the mainthread calls wait() somehow the workingthread's event queue is not dispatched anymore, but I don't understand why, I thought the workingthread has a different evene queue.

    It would be nice if you can explain me, why this happens and how I can fix it.
    Alternative solutions for my problem would be nice, too!

    Thanks for help in advance!

  2. #2
    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: Calling QThread.wait() from mainthread

    Quote Originally Posted by P@u1 View Post
    The wait() call is needed to prevent the main thread from deleting the workerThread object, while it is still running...
    You just need to tell the thread to quit() before you start deleting it.
    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.


  3. #3
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling QThread.wait() from mainthread

    Remember that slots are always called by that thread where the slot's object lives.

    So, in your case you have probably created the WorkerThread in the main thread and then you have started it with start()
    Qt Code:
    1. // executing in main thread
    2. worker = new WorkerThread; // created in main thread so it lives in it too
    3. worker->start();
    To copy to clipboard, switch view to plain text mode 
    Now, when timeout() signal is emitted in the worker thread the main thread would call the update() slot if the execution would be inside the event loop. But, since the execution is currently blocked inside the wait() call, nothing happens.

    To fix this, the WorkerThread object would need to live in the worker thread.

    I'll modify your class a bit:
    Qt Code:
    1. class Worker : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. ~Worker();
    6. private slots:
    7. void run();
    8. void update();
    9. private:
    10. volatile bool m_isInterrupted;
    11. };
    To copy to clipboard, switch view to plain text mode 
    Then in main thread where you created the worker:
    Qt Code:
    1. // executing in main thread
    2. thread = new QThread;
    3. worker = new Worker; // created in main thread so it lives in it too
    4. worker->moveToThread( thread ); // change thread where the worker lives
    5. thread->start();
    6. QTimer::singleShot( 0, worker, SLOT(run()) ); // ask 'thread' to call the run() slot
    To copy to clipboard, switch view to plain text mode 
    Last edited by joyer83; 1st June 2011 at 15:45.

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

    P@u1 (8th June 2011)

  5. #4
    Join Date
    Jun 2011
    Posts
    38
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: Calling QThread.wait() from mainthread

    Quote Originally Posted by wysota View Post
    You just need to tell the thread to quit() before you start deleting it.
    Thats exactly what i'm trying.
    I set a flag which causes the thread to call quit, but it will need a short time until the thread does that and i must wait until it has been done so i use wait().

    But unfortunately my approach causes the mentioned problems.
    It would be nice if you can provide some code.
    Thanks in advance.

    Edit: just as a note: while i was writing this there was another post
    @joyer83: thx for help! I will try this.
    Last edited by P@u1; 1st June 2011 at 16:26.

Similar Threads

  1. how to resume QThread::wait()?
    By naturalpsychic in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2011, 23:07
  2. QThread::wait() causing crash
    By Olliebrown in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2010, 15:24
  3. Replies: 0
    Last Post: 26th February 2010, 19:01
  4. QThread blocking wait()
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2009, 09:21
  5. Calling QThread::exec() repeatedly
    By hb in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2007, 20:24

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.