[Day3] Read Rust Atomics and Locks

by Mara Bos

R4 Cheng
1 min readApr 17, 2023

At Topics: Chapter 1. Basics of Rust Concurrency

until: Thread Builder

Sync to My Blog: https://my-blog-chengr4.vercel.app/posts/day3-read-rust-atomics-and-locks_230409

Recall

  • What is .join() ? => make sure the threads are finished before we return from main
fn main() {
let t1 = thread::spawn(f);

// use JoinHandle to ahcieve it
// The .join() method waits until the thread has finished executing
// and returns a std::thread::Result.
// unwrap() is for panic
t1.join().unwrap();
}

Notes

  • The std::thread::spawn function is actually just a convenient shorthand for std::thread::Builder::new().spawn().unwrap()

Eg:

let numbers = Vec::from_iter(0..=1000);

let t = thread::spawn(move || {
let len = numbers.len();
let sum = numbers.iter().sum::<usize>();
sum / len
});

let average = t.join().unwrap();

println!("average: {average}");
  • A std::thread::Builder can: eg set size, give thread a name
  • The std::thread::spawn function simply panics if it is unable to spawn a new thread => by std::io::Result

Not Understand Yet

  • for loop in closure: &number vs number
let numbers = vec![1, 2, 3];

thread::spawn(move || {
for n in &numbers { // <== here
println!("{n}");
}
}).join().unwrap();

// vs

let numbers = vec![1, 2, 3];

thread::spawn(move || {
for n in numbers { // <== here
println!("{n}");
}
}).join().unwrap();

--

--

R4 Cheng
R4 Cheng

Written by R4 Cheng

「0」が過去で「1」が未来「今」は何処にもない

No responses yet