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