How does Rust handle killing threads?

痞子三分冷 提交于 2020-01-11 10:41:47

问题


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:


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 an Err containing the argument given to panic.

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.



来源:https://stackoverflow.com/questions/55228629/how-does-rust-handle-killing-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!