Waker Struct
Waker struct สำหรับ async runtime notifications ใน Rust เป็นกลไกที่ futures ใช้บอก runtime ว่าพร้อมที่จะถูก poll อีกครั้ง
1. Waker Basics
Section titled “1. Waker Basics”Waker เป็น handle ที่ async runtime ส่งให้ futures สำหรับการ wake up task เมื่อ ready
How Waker Works
Section titled “How Waker Works”use std::task::Poll;
fn main() { // Waker is how futures notify the runtime they're ready // The pattern works like this:
// 1. Runtime polls a future // 2. If not ready, future returns Poll::Pending // 3. Future stores cx.waker().clone() // 4. Later, when data arrives (I/O, timer, etc.) // 5. Future calls waker.wake() // 6. Runtime polls the future again
println!("=== Waker Lifecycle ==="); println!("1. Poll future -> Pending"); println!("2. Future stores waker"); println!("3. Event occurs (I/O ready)"); println!("4. waker.wake() called"); println!("5. Runtime polls again -> Ready");}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google