知识卡片

普通读书笔记卡 · 10649 · 生成模型:未记录

多生产者共享接收端

理解等级:中级

mpsc 通道有一端接收,可以有多端发送。调用 channel 得到 tx、rx 后,克隆 tx 并将每个发送句柄移给相应线程,就能用同一个 rx 汇总结果。来自不同线程的消息可交错,具体顺序取决于调度。有未释放的发送句柄时,接收者仍应预期以后可能收到消息。

相关卡片 [[通道接收与关闭]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第16章 无畏并发(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c16.xhtml`,块 0120) - 结论依据:原文展示把克隆句柄和原句柄分别交给两个线程,最后从一个接收端获取全部消息。 - 原始内容:This time, before we create the first spawned thread, we call clone on the transmitter. This will give us a new transmitter we can pass to the first spawned thread. We pass the original transmitter to a second spawned thread. This gives us two threads, each sending different messages to the one receiver.