知识卡片
线程调度与进程退出
Rust 标准库把每个语言线程映射到一个操作系统线程。thread::spawn 接受新线程要执行的闭包,操作系统决定它何时运行。主线程和子线程中添加 sleep 可增加交错机会,程序需要顺序保证时应显式同步。主线程返回使整个进程结束,未完成的子线程也会停止。
相关卡片 [[等待线程的时机]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 无畏并发 Using Threads to Run Code Simultaneously · 查看原书依据
Note that when the main thread of a Rust program completes, all spawned threads are shut down, whether or not they have finished running. The output from this program might be a little different every time, but it will look similar to the following:
参考来源
- 位置:《The Rust Programming Language(第2版)》第16章 无畏并发(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c16.xhtml`,块 0026)
- 结论依据:原文说明了主线程结束时所有新建线程的命运,并展示了每次次序可能变化的输出。
- 原始内容:Note that when the main thread of a Rust program completes, all spawned threads are shut down, whether or not they have finished running. The output from this program might be a little different every time, but it will look similar to the following: