Results 1 to 4 of 4

Thread: Terminating a thread.

  1. #1
    Join Date
    Dec 2006
    Posts
    17
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Terminating a thread.

    Hi ,

    I have a thread created in main thread which is created in the heap.

    Up on some user input i need to terminate this thread.

    I have written a fuction TerminateThread

    TerminateThread()
    {
    m_mutex.lock()
    m_flag = true; // m_mutex and m_flag are global. The other thread will check this flag after some operations, if the flag is set ,it will call terminate( return).
    m_mutex.unlock();
    delete m_thread ;
    //Delete statement is giving an error saying trying to delete a running thread.

    So i replaced the code with this
    // Since after the thread terminates m_thread->isRunning() = false
    while ( m_thread->isRunning()) {
    wait(4000); //This is making the thread to wait indefinetly.
    }

    }

    I have also tried
    m_thread->terminate()
    m_thread->wait(); // Never returns from the wait call
    But even this is not working.

    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Terminating a thread.

    You will have to make the thread to return from QThread::run(). Please search the forums, this subject has been discussed quite a few times.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Terminating a thread.

    You could be running into a kernel bug:

    https://bugzilla.novell.com/show_bug.cgi?id=289641

    Have you tried compiling Qt with no exceptions? (-no-exceptions).
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  4. #4
    Join Date
    Jan 2007
    Location
    Augsburg, Germany
    Posts
    75
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Terminating a thread.

    If you simply start "exec()" in your "thread::run()" method you can stop the thread by calling "quit()" and the "wait()" method would work

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  3. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  4. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44
  5. Replies: 2
    Last Post: 6th January 2006, 21:15

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.