Is there a parent-child connection between threads that are spawned? If I kill the thread from where I spawned other threads, are those going to get killed too? Is this OS specific?
1 Answers
How does Rust handle killing threads?
It doesn't; there is no way to kill a thread.
See also:
- How to terminate or suspend a Rust thread from another thread?
- How to check if a thread has finished in Rust?
Is there a parent-child connection between threads that are spawned?
When you spawn a thread, you get a JoinHandle
that allows you to wait for the child thread to finish. The child does not know of the parent.
[what happens to the other threads] in the context of a thread panicking and dying
The documentation for thread::spawn
covers this well:
The join handle will implicitly detach the child thread upon being dropped. In this case, the child thread may outlive the parent (unless the parent thread is the main thread; the whole process is terminated when the main thread finishes). Additionally, the join handle provides a
join
method that can be used to join the child thread. If the child thread panics,join
will return anErr
containing the argument given topanic
.
That is, once a child thread has been started, what happens to the parent thread basically doesn't matter, unless the parent thread was the main thread, in which case the entire process is terminated.

- 388,571
- 95
- 1,107
- 1,366
-
If I kill the thread that spawns other threads, what happen to the other threads ? – ludeed Mar 18 '19 at 19:42
-
1@LudeeD there is to way to kill a thread in Rust, so the answer is [*mu*](http://wiki.c2.com/?MuAnswer). – Shepmaster Mar 18 '19 at 19:48
-
Can you not "drop" it then? – cloudsurfin Aug 16 '23 at 00:02