Add code
This commit is contained in:
23
example_8/src/main.rs
Normal file
23
example_8/src/main.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use std::rc::Rc; // reference-counted, non atomic
|
||||
use std::sync::Arc; // reference-counted, atomic
|
||||
|
||||
fn main() {
|
||||
// this will not compile:
|
||||
let rc = Rc::new("not thread safe");
|
||||
std::thread::spawn(move || {
|
||||
println!("I have an rc with: {}", rc);
|
||||
});
|
||||
|
||||
// this compiles fine:
|
||||
let arc = Arc::new("thread safe");
|
||||
std::thread::spawn(move || {
|
||||
println!("I have an arc with: {}", arc);
|
||||
});
|
||||
|
||||
// this will also not compile:
|
||||
let mut v = Vec::new();
|
||||
std::thread::spawn(|| {
|
||||
v.push(42);
|
||||
});
|
||||
let _ = v.pop();
|
||||
}
|
||||
Reference in New Issue
Block a user