知识卡片
等待线程的时机
JoinHandle 代表由 spawn 启动的线程。调用 join 会阻塞调用方直到该线程结束。在主线程的其他工作之后 join,双方先有机会并行推进,再保证退出前等待完成。在其他工作之前 join,主线程的那段工作要等子线程完成后才开始。示例用 unwrap 处理 join 的结果,子线程恐慌时不具备正常返回的保证。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 无畏并发 Waiting for All Threads to Finish Using join Handles · 查看原书依据
Calling join on the handle blocks the thread currently running until the thread represented by the handle terminates. Blocking a thread means that thread is prevented from performing work or exiting. Because we’ve put the call to join after the main thread’s for loop, running Listing 16-2 should produce output similar to this:
参考来源
- 位置:《The Rust Programming Language(第2版)》第16章 无畏并发(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c16.xhtml`,块 0036)
- 结论依据:原文解释了 join 的阻塞语义,后续调整调用位置得到先后执行的输出。
- 原始内容:Calling join on the handle blocks the thread currently running until the thread represented by the handle terminates. Blocking a thread means that thread is prevented from performing work or exiting. Because we’ve put the call to join after the main thread’s for loop, running Listing 16-2 should produce output similar to this: