Rust如何处理终止线程? [英] How does Rust handle killing threads?

查看:709
本文介绍了Rust如何处理终止线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在产生的线程之间是否存在父子连接?
如果我从产生其他线程的位置杀死该线程,那些线程也将被杀死吗?

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.

另请参见:

  • 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 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.

这篇关于Rust如何处理终止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆