When I spawn
a thread in Rust, I get a JoinHandle
, which is good for... joining (a blocking operation), and not much else. How can I check if a child thread has exited (i.e., JoinHandle.join()
would not block) from the parent thread? Bonus points if you know how to kill a child thread.
I imagine you could do this by creating a channel, sending something to the child, and catching errors, but that seems like needless complexity and overhead.
Use Thread. Join(TimeSpan. Zero) It will not block the caller and returns a value indicating whether the thread has completed its work.
The threads provided by the Rust standard library are "OS threads", that is, they use the facilities of your operating system. Therefore, a Rust program has no limit imposed by Rust itself, but rather, this limit would result from whatever your OS lets you do.
Short Answer: When a thread has finished its process, if nothing else holds a reference to it, the garbage collector will dispose of it automatically.
The threading modelAn executing Rust program consists of a collection of native OS threads, each with their own stack and local state. Threads can be named, and provide some built-in support for low-level synchronization.
As of Rust 1.7, there's no API in the standard library to check if a child thread has exited without blocking.
A portable workaround would be to use channels to send a message from the child to the parent to signal that the child is about to exit. Receiver
has a non-blocking try_recv
method. When try_recv
does receive a message, you can then use join()
on the JoinHandle
to retrieve the thread's result.
There are also unstable platform-specific extension traits that let you obtain the raw thread handle. You'd then have to write platform-specific code to test whether the thread has exited or not.
If you think this feature should be in Rust's standard library, you can submit an RFC (be sure to read the README first!).
Bonus points if you know how to kill a child thread.
Threads in Rust are implemented using native OS threads. Even though the operating system might provide a way to kill a thread, it's a bad idea to do so, because the resources that the thread allocated will not be cleaned up until the process ends.
It's possible, friends. Use refcounters which Rust will drop on end or panic. 100% safe. Example:
use std::time::Duration; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::thread; fn main() { // Play with this flag let fatal_flag = true; let do_stop = true; let working = Arc::new(AtomicBool::new(true)); let control = Arc::downgrade(&working); thread::spawn(move || { while (*working).load(Ordering::Relaxed) { if fatal_flag { panic!("Oh, my God!"); } else { thread::sleep(Duration::from_millis(20)); println!("I'm alive!"); } } }); thread::sleep(Duration::from_millis(50)); // To stop thread if do_stop { match control.upgrade() { Some(working) => (*working).store(false, Ordering::Relaxed), None => println!("Sorry, but thread died already."), } } thread::sleep(Duration::from_millis(50)); // To check it's alive / died match control.upgrade() { Some(_) => println!("Thread alive!"), None => println!("Thread ends!"), } }
Gist: https://gist.github.com/DenisKolodin/edea80f2f5becb86f718c330219178e2
At playground: https://play.rust-lang.org/?gist=9a0cf161ba0bbffe3824b9db4308e1fb&version=stable&backtrace=0
UPD: I've created thread-control
crate which implements this approach: https://github.com/DenisKolodin/thread-control
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With