Results 1 to 2 of 2

Thread: gracefully exit thread when working with 3rd party lib for which I don't have control

  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default gracefully exit thread when working with 3rd party lib for which I don't have control

    I am using QThread to do my work where I am calling some function in the 3rd party library (So I don't have any control here to put some flags & quit my thread).
    Is there a way to forcefully kill thread while it is doing some heavy work.

    I have below example to explain my requirement;

    Qt Code:
    1. //My worker class
    2. void worker::myWork()
    3. {
    4. QThread::sleep(10);
    5. }
    6.  
    7.  
    8. //main class
    9. Widget::Widget()
    10. {
    11. // QThread mythread & worker m_worker; are the class members of Widget
    12.  
    13. m_worker.moveToThread(&mythread);
    14. connect(&mythread, SIGNAL(started()), &m_worker, SLOT(myWork()));
    15. mythread.start();
    16.  
    17. killThread();
    18. }
    19.  
    20. void Widget::killThread()
    21. {
    22. QThread::sleep(3);
    23. mythread.terminate();
    24. }
    To copy to clipboard, switch view to plain text mode 

    In the above code, I am trying to start thread which run for 9 sec & then immediately calling a function which trying to terminate a thread in 3 sec, But the thread is terminating after 9 seconds only not immediately, so my question here is can I kill the thread in 3 sec only.


    I don't want to wait till thread finish as below (which very well may take few minutes in my application).

    if(m_thread.isRunning())
    {
    m_thread.quit();
    m_thread.wait();
    }
    Thanks :-)

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

    Default Re: gracefully exit thread when working with 3rd party lib for which I don't have con

    Since thread's cannot be cleanly terminated or rather their resources cannot be cleanly freed when terminated, your best option is to move the uninterruptible 3rd party calculation into a helper program which you then run as a child process.

    Processes can be cleanly "removed" by the operating system.

    Cheers,
    _

Similar Threads

  1. QThread - Using a slot to exit the thread
    By johnnyturbo3 in forum Qt Programming
    Replies: 7
    Last Post: 27th April 2011, 11:36
  2. Replies: 4
    Last Post: 16th February 2010, 22:25
  3. Thread and GUI control
    By foxyproxy in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 19:52
  4. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 15:31
  5. Control-C to interrupt is not working
    By rburge in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2006, 21:18

Tags for this Thread

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.