[Day2] Read Rust Atomics and Locks

by Mara Bos

R4 Cheng
1 min readMar 4, 2023

At Topics

until: before Output Locking

Notes

  • A process should always ask OS kernel first and then interact with another process
  • in Rust, to spawn a new thread:
use std::thread;

fn main(){
thread::spawn(f);
}

fn f() {
// ...function to do
}
  • The Rust standard library assigns every thread a unique identifier. This identifier is accessible through Thread::id() and is of the type ThreadId. Rach ID will be different but not consecutively
  • Returning from main thread will exit the entire program, even if other threads are still running.
// To make sure the threads are finished before we return from main

fn main() {
let t1 = thread::spawn(f);
let t2 = thread::spawn(f);

println!("Hello from the main thread.");

// 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();
t2.join().unwrap();
}

在 Rust 中,”panic” 是一種用於處理錯誤的機制。當程式遇到無法處理的錯誤時,它會拋出一個 panic,並停止執行

--

--

R4 Cheng
R4 Cheng

Written by R4 Cheng

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

No responses yet