๊ฒฝ๊ณ ์ฑ๋
With bounded (synchronous) channels, send
can 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!("๋ฉ์์ง {i}")).unwrap(); println!("{thread_id:?}: ๋ณด๋ธ ๋ฉ์์ง {i}"); } println!("{thread_id:?}: ์๋ฃ"); }); thread::sleep(Duration::from_millis(100)); for msg in rx.iter() { println!("๊ธฐ๋ณธ: {msg} ๋ฐ์"); } }
send
๋ฅผ ํธ์ถํ๋ฉด ์ฑ๋์ ์ ๋ฉ์์ง๋ฅผ ์ํ ๊ณต๊ฐ์ด ํ๋ณด๋ ๋๊น์ง ํ์ฌ ์ค๋ ๋๊ฐ ์ฐจ๋จ๋ฉ๋๋ค. ์ฑ๋์์ ์ฝ๋ ์ฌ๋์ด ์๋ ๊ฒฝ์ฐ ์ค๋ ๋๊ฐ ๋ฌด๊ธฐํ ์ฐจ๋จ๋ ์ ์์ต๋๋ค.send
ํธ์ถ์ ์ค๋ฅ์ ํจ๊ป ์ค๋จ๋ฉ๋๋ค. ๋ฐ๋ผ์ ์ฑ๋์ด ๋ซํ๋ฉดResult
๋ฅผ ๋ฐํํฉ๋๋ค. ์์ ์๋ฅผ ์ญ์ ํ๋ฉด ์ฑ๋์ด ๋ซํ๋๋ค.- A bounded channel with a size of zero is called a โrendezvous channelโ. Every send will block the current thread until another thread calls
recv
.