Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sync & Send

Marker traits สำหรับ concurrency

Send

Type ที่ส่งระหว่าง threads ได้:

#![allow(unused)]
fn main() {
// Most types are Send
// Rc<T> is NOT Send (use Arc<T> instead)
}

Sync

Type ที่หลาย threads เข้าถึงพร้อมกันได้:

#![allow(unused)]
fn main() {
// T is Sync if &T is Send
// RefCell<T> is NOT Sync
// Mutex<T> IS Sync
}

สรุปบทที่ 15

แนวคิดใช้เมื่อ
thread::spawnสร้าง thread
channelส่งข้อมูลระหว่าง threads
Mutexป้องกัน data race
Arcแชร์ ownership ระหว่าง threads

👉 ต่อไป: บทที่ 16: Async/Await