知识卡片
通道接收与关闭
接收端调用 recv 会等新消息或等到通道断开,适合等待期间无其他工作的线程。try_recv 立即返回,调用者可区分暂时无消息和断开。把接收端作为迭代器时,循环隐式等待每条消息,全部发送端释放且积压处理完后结束。接收端先丢弃时,发送端调用 send 会得知失败。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 无畏并发 Using Message Passing to Transfer Data Between Threads · 查看原书依据
The receiver has two useful methods: recv and try_recv. We’re using recv, short for receive, which will block the main thread’s execution and wait until a value is sent down the channel. Once a value is sent, recv will return it in a Result<T, E>. When the transmitter closes, recv will return an error to signal that no more values will be coming.
参考来源
- 位置:《The Rust Programming Language(第2版)》第16章 无畏并发(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c16.xhtml`,块 0091)
- 结论依据:recv 的阻塞语义、消息可用时的返回值及发送端关闭后的错误在该段给出,同节补充 try_recv 和迭代。
- 原始内容:The receiver has two useful methods: recv and try_recv. We’re using recv, short for receive, which will block the main thread’s execution and wait until a value is sent down the channel. Once a value is sent, recv will return it in a Result<T, E>. When the transmitter closes, recv will return an error to signal that no more values will be coming.