Results 1 to 8 of 8

Thread: can not stop QThread

  1. #1
    Join Date
    Jul 2015
    Posts
    26
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default can not stop QThread

    I am using Qthread/Worker in Qt4.8. In the worker, a third-party library is called.
    The code worked fine before. Now qthread can not be terminated after data structure
    is changed for the call to the third-party library. What can be the cause?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can not stop QThread

    There is not enough information to tell what the problem could be, but maybe the third party function does not return?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    26
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can not stop QThread

    it is standard Qthread/Worker model. 3rd party does not return anything. terminate is used to stop the qthread. But qthread keeps running until 3rd party code is finished.
    The same code without any change works fine and Qthread stops quickly.


    Added after 1 43 minutes:


    the problem is found. I upgraded qt from 4.7.1.to 4.8.6. Qthread can stop quickly with 4.7.1, but not with 4.8.6.
    I compared this with the same code. Any ideas?
    Last edited by newcfd; 13th December 2015 at 20:33.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can not stop QThread

    3rd party does not return anything.
    I was not asking what it returns, I was asking it it returns.

    When you call QThread::quit() to end the thread's event loop, it will do so once the 3rd party code that it currently executes returns.

    You can think of this as setting a flag, which the thread will check and react on at its first possible opportunity.
    As long as it is executing 3rd party code it is not yet in that situation.

    So one possible cause for the thread not ending when you expect it to end is that it is still executing the 3rd party code and has not reached the check yet.

    Always remember that a thread needs to cooperate when being told to stop as there is no safe way of terminating it like one could terminate a process.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    26
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can not stop QThread

    Thanks for your reply. I got what you wrote. There is no flag in the third party.
    If Qt4.8.6 is used, the third party does not return right away and keeps running instead
    after Qthread.terminate() is called . But with 4.7.1, the thread stops right away and
    obviously the third party returns immediately.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can not stop QThread

    Oh, you are literally calling QThread::terminate().

    That is a very, very bad idea if you want your program to continue afterwards.

    Terminating a thread like that leaves your application in an unknown state!
    Any memory allocated by the thread up to this point will be leaked, locks will be held indefinitely, etc.

    The only safe way of preemptively ending a parallel execution context is to put it into a helper program and run it as a child process.
    Process resources, such as memory, are tracked by the operating system and can thus be released upon termination.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2015
    Posts
    26
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can not stop QThread

    Thanks for your explanation. I know this problem.
    It looks like the third party code can handle this issue.

    Now the question is
    why QThread in 4.8.6 does not work properly while QThread in 4.7.1 is fine
    in my application? The code is exactly same.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can not stop QThread

    Have you tried terminating an "empty" thread?
    Or a thread that just runs a loop with some sleep in it?

    Cheers,
    _

Similar Threads

  1. QThread : how to stop an infinite loop
    By TarielVincent in forum Qt Programming
    Replies: 9
    Last Post: 24th February 2012, 22:22
  2. QThread don' stop (or the signal don't emit)
    By msdark in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2011, 00:51
  3. How to stop QThread?
    By vespasianvs in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2010, 07:42
  4. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 14:33
  5. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 08: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.