Why and when shouldn't I kill a thread?

后端 未结 6 695
陌清茗
陌清茗 2020-12-15 13:35

I am writing a multithreaded socket server and I need to know for sure.

Articles about threads say that I should wait for the thread to return, instead of killing it

相关标签:
6条回答
  • 2020-12-15 13:38

    There's many reasons, but here's an easy one: there's only one heap. If a thread allocates ANYTHING on the heap, and you kill it, whatever it has allocated is around until the process ends. Each thread gets its own stack, and so that MAY be freed (implementation-dependent), but you GUARANTEE leaks on the heap by not letting it shut itself down.

    0 讨论(0)
  • 2020-12-15 13:43

    In the case of a thread blocked in I/O you never really need to kill it, instead you have the choice between non-blocking I/O, timeouts, and closing the socket from another thread. Either of these will unblock the thread.

    0 讨论(0)
  • 2020-12-15 13:46

    Killing a thread means stopping all execution exactly where it is a the moment. In particular, it will not execute any destructors. This means sockets and files won't be closed, dynamically-allocated memory will not be freed, mutexes and semaphores won't be released, etc. Killing a thread is almost guaranteed to cause resource leaks and deadlocks.

    Thus, your question is kind of reversed. The real question should read:

    When, and under what conditions can I kill a thread?

    So, you can kill the thread when you're convinced no leaks and deadlocks can occur, not now, and not when the other thread's code will be modified (thus, it is pretty much impossible to guarantee).


    In your specific case, the solution is to use non-blocking sockets and check some thread/user-specific flag between calls to send()and recv(). This will likely complicate your code, which is probably why you've been resisting to do so, but it's the proper way to go about it.

    Moreover, you will quickly realize that a thread-per-client approach doesn't scale, so you'll change your architecture and re-write lots of it anyways.

    0 讨论(0)
  • 2020-12-15 13:54

    You really don't want to do this.

    If you kill a thread while it holds a critical section it won't be released which will likely result in your whole application breaking. Certain C library calls like heap memory allocation use critical sections and if you happen to kill your thread while it's doing a "new" then calling new from anywhere else in your program will cause that thread to stop.

    You simply can't do this safely without really extreme measures which are much more restrictive than simply signalling the thread to terminate itsself.

    0 讨论(0)
  • 2020-12-15 13:55

    Killing a thread can cause your program to leak resources because the thread did not get a chance to clean up after itself. Consider closing the socket handle the thread is sending on. This will cause the blocking send() to return immediately with an appropriate error code. The thread can then clean up and die peacefully.

    0 讨论(0)
  • 2020-12-15 14:02

    If you kill your thread the hard way it can leak resources.

    You can avoid it when you design your thread to support cancelation.

    Do not use blocking calls or use blocking calls with a timeout. Receive or send data in smaller chunks or asynchronously.

    0 讨论(0)
提交回复
热议问题