Rust如何处理终止线程? [英] How does Rust handle killing threads?
问题描述
在产生的线程之间是否存在父子连接?
如果我从产生其他线程的位置杀死该线程,那些线程也将被杀死吗?
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?
推荐答案
Rust如何处理杀死线程?
How does Rust handle killing threads?
不是;没有办法杀死线程。
It doesn't; there is no way to kill a thread.
另请参见:
- < a href = https://stackoverflow.com/q/26199926/155423>如何从另一个线程终止或暂停Rust线程?
- 如何检查Rust中的线程是否已完成?
- 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?
生成时一个线程,您将得到一个 JoinHandle
允许您等待子线程完成。孩子不认识父母。
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
thread :: spawn
可以很好地说明这一点:
The documentation for thread::spawn
covers this well:
在删除
后,联接句柄将隐式地 detach 子线程。在这种情况下,子线程可能会超过父线程(除非
父线程是主线程;当
主线程完成时,整个过程将终止)。此外,联接句柄还提供了join
方法,可用于加入子线程。如果子线程
出现紧急情况,请join
将返回Err
包含为
提供的参数panic
。
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.
这篇关于Rust如何处理终止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!