Hello all
So I was wondering why when I filter for the threads created by thread::spawn
show up as green threads in htop? A minimal working example is -
use std::thread;
use std::time::Duration;
use std::sync::mpsc;
fn main() {
let (tx, rx) = mpsc::channel();
let tx2 = tx.clone();
// Create two threads and print the outputs
let t1 = thread::spawn(move || {
let msgs = vec![String::from("First thread msg 1"),
String::from("First thread msg 2"),
String::from("First thread msg 3"),
String::from("First thread msg 4")];
for val in msgs {
tx.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
let t2 = thread::spawn(move || {
let msgs = vec![
String::from("Second thread msg 1"),
String::from("Second thread msg 2"),
String::from("Second thread msg 3"),
String::from("Second thread msg 4"),
];
for val in msgs {
tx2.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
for r in rx {
println!("Got: {}", r);
}
t1.join().unwrap();
t2.join().unwrap();
}
It produces the output expected -
mintycocoa@TheTreehouse:/tmp$ ./mwe_threads
Got: Second thread msg 1
Got: First thread msg 1
Got: Second thread msg 2
Got: First thread msg 2
Got: Second thread msg 3
Got: First thread msg 3
Got: Second thread msg 4
Got: First thread msg 4
However during it's execution, if I filter htop
by the text mwe_threads
and see the processes I get -
One real process with two green threads. And just to ensure that this is in fact a green thread, I told htop to hide all green (user) threads by pressing H
and I get -
I've heard that rust only has OS threads (so 1-to-1 threads or processes), but it seems to be creating green threads. I'm just beginning learning concurrency in rust, so I may be misunderstanding. Could someone explain why I'm seeing this?