Powered by
> whoami michele.damico@gmail.com https://github.com/la10736 https://www.linkedin.com/in/damico/ @PhenoCoder
Sono ottimi strumenti per garantire in fase di compilazione un accesso sicuro alla memoria ....
Sono ottimi strumenti per garantire in fase di compilazione un accesso sicuro e concorrente alla memoria!
spawn
std::thread
std::sync::mpsc
let (tx, rx) = std::sync::mpsc::channel();
tx.send("hi").unwrap();
assert_eq!("hi", rx.recv().unwrap());std::sync::Mutex
std::sync::
Send
Sync
inviabile in modo sicuro
condivisibile in modo sicuro
il riferimento è inviabile in modo sicuro
std::thread::spawn()
let handle = std::thread::spawn(|| {
println!("Hi from thread")
});
handle.join().unwrap();
handle: std::thread::JoinHandle<()>
Valori di ritorno: Quando si esegue la join viene tornato il risultato della closure a meno di errori
let handle= std::thread::spawn(|| {
"hi from Rust!"
});
assert_eq!("hi from Rust!", handle.join().unwrap());
let handle= std::thread::spawn(|| {
42
});
assert_eq!(42, handle.join().unwrap());
handle: std::thread::JoinHandle<&str>
handle: std::thread::JoinHandle<i32>
Gestione degli errori: un panic nel thread non influenza il resto dell' esecuzione ma viene catturato dal JoinHandle.
let handle= std::thread::spawn(|| {
"aa".parse::<i32>().unwrap()
});
assert!(handle.join().is_err());
println!("I'm done");
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an
`Err` value: ParseIntError { kind: InvalidDigit }',
/checkout/src/libcore/result.rs:906:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.
I'm done
Process finished with exit code 0
std::sync::mpsc::channel()
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || tx.send(42).unwrap());
assert_eq!(42, rx.recv().unwrap());tx: std::sync::mpsc::Sender<i32>
rx: std::sync::mpsc::Receiver<i32>
Ownership
let (tx, rx) = std::sync::mpsc::channel();
let obj = "Object".to_string();
tx.send(obj);
println!("{}", obj);
--> src/main.rs:7:20
|
5 | tx.send(obj);
| --- value moved here
6 |
7 | println!("{}", obj);
| ^^^ value used here after move
|
= note: move occurs because `obj` has type `std::string::String`,
which does not implement the `Copy` trait
Multiple Producers
let (tx, rx) = std::sync::mpsc::channel();
for i in 1..10 {
let producer = tx.clone();
std::thread::spawn(move || producer.send(i).unwrap());
}
drop(tx);
while let Ok(x) = rx.recv() {
print!("{} ", x);
}E' Possibile clonare il Sender<T> per ottenere più sorgenti collegate allo stesso consumer.
4 3 2 7 1 6 8 9 5
Process finished with exit code 0Receiver come iteratori
let (tx, rx) = std::sync::mpsc::channel();
for i in 1..10 {
let producer = tx.clone();
std::thread::spawn(move || producer.send(i).unwrap());
}
drop(tx);
for i in rx.into_iter() {
print!("{} ", i);
}
E' possibile trasformare il Receiver<T> in un iteratore e ricevere i dati iterando.
1 4 3 6 5 2 7 8 9
Process finished with exit code 0std::sync::Mutex
let data = std::sync::Mutex::new(String::new());
println!("START");
{
let mut buffer = data.lock().unwrap();
buffer.push_str("Message: ");
}
println!("FIRST");
{
let mut buffer = data.lock().unwrap();
buffer.push_str("Hello!");
}
println!("SECOND");
let content = data.lock().unwrap();
assert_eq!("Message: Hello!", *content);
println!("DONE! -> {}", *content)
START
FIRST
SECOND
DONE! -> Message: Hello!
Provate a togliere un po di parentesi e guardate cosa succede