知识卡片
盒装一次性任务
将 Job 定义为 Box,把各种具体闭包装入大小已知的盒子。execute 把盒子通过通道转给工作线程。工作线程取出后调用 job(),消费这个一次性任务。发送返回 Result,若接收端已全部退出,应由调用侧决定如何处理失败。
相关卡片 [[跨线程任务的闭包约束]]、[[特征对象统一异构组件]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 最终项目:构建多线程 Web 服务器 Creating Space to Store the Threads · 查看原书依据
--snip-- type Job = Box<dyn FnOnce() + Send + 'static>; impl ThreadPool { --snip-- pub fn execute<F>(&self, f: F) where F: FnOnce() + Send + 'static, { ❶ let job = Box::new(f); ❷ self.sender.send(job).unwrap(); } } --snip--
参考来源
- 位置:《The Rust Programming Language(第2版)》第20章 最终项目:构建多线程 Web 服务器(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c20.xhtml`,块 0245)
- 结论依据:清单同时展示 Job 的统一类型和 execute 内的装箱、发送操作。
- 原始内容:--snip-- type Job = Box<dyn FnOnce() + Send + 'static>; impl ThreadPool { --snip-- pub fn execute<F>(&self, f: F) where F: FnOnce() + Send + 'static, { ❶ let job = Box::new(f); ❷ self.sender.send(job).unwrap(); } } --snip--