Results 1 to 4 of 4

Thread: How to destroy or terminate a thread ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to destroy or terminate a thread ?

    I have a program that every time a client connects to the server, a new thread is created for that client.

    When the login has been unsuccessful, then the server should destroy the thread that was created to handle that client.

    How do I destroy the thread or terminate it ? so when the client wants to try to login again, a new thread will handle it ?

  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: How to destroy or terminate a thread ?

    The execution of the thread ends when the control returns from QThread::run(). If the thread is running an event loop, it can be quit by QThread::quit(). It might be a good idea to delete the thread object as well, at least if the server is expected to run for a long time. Otherwise the memory is not freed until the application quits (supposing the thread objects are children of the server object).
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to destroy or terminate a thread ?

    My application consists of a server an a client. The server creates a thread every time a new client connects to it. This created thread handles all the logic with the client.

    When the client disconnects, then the thread should stop and also be deleted from memory.

    How ?

  4. #4
    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: How to destroy or terminate a thread ?

    Simply return from run(). Just remember to delete the thread object if it was created on heap or you may starve your system. Alternatively you could implement a queue of threads waiting for requests to be handled instead of spawning new threads each time and terminating them afterwards.

Similar Threads

  1. How to destroy a thread ?
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2006, 18:04
  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.