Bounded Channels

๊ฒฝ๊ณ„๊ฐ€ ์žˆ๋Š” ๋™๊ธฐ ์ฑ„๋„์€ send๊ฐ€ ์ฃผ ์Šค๋ ˆ๋“œ๋ฅผ ์ฐจ๋‹จํ•˜๋„๋ก ๋งŒ๋“ญ๋‹ˆ๋‹ค:

Bounded and synchronous channels make send block the current thread:

use std::sync::mpsc;
use std::thread;
use std::time::Duration;

fn main() {
    let (tx, rx) = mpsc::sync_channel(3);

    thread::spawn(move || {
        let thread_id = thread::current().id();
        for i in 1..10 {
            tx.send(format!("Message {i}")).unwrap();
            println!("{thread_id:?}: sent Message {i}");
        }
        println!("{thread_id:?}: done");
    });
    thread::sleep(Duration::from_millis(100));

    for msg in rx.iter() {
        println!("Main: got {}", msg);
    }
}