如何指定线程不应该超过它们创建的函数?

时间:2017-02-13 01:14:46

标签: multithreading rust

我正在尝试解决玩具在平行中找到切片中最长字符串的问题。但是我遇到了以下编译器错误:

error[E0495]: cannot infer an appropriate lifetime for autoref due
to conflicting requirements
  --> main.rs:10:33
   |
10 |     for assigned_lines in input.chunks(max_lines_per_worker as usize) {
   |                                 ^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1
defined on the body at 5:59...
  --> main.rs:5:60
   |
5  | pub fn max_length(input: &[&str], workers: usize) -> usize {
   |                                                            ^
note: ...so that reference does not outlive borrowed content
  --> main.rs:10:27
   |
10 |     for assigned_lines in input.chunks(max_lines_per_worker as usize) {
   |                           ^^^^^
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@main.rs:11:36: 19:10 assigned_lines:&[&str]]` will meet its required lifetime bounds
  --> main.rs:11:22
   |
11 |         let handle = thread::spawn(move || {
   |                      ^^^^^^^^^^^^^

如果我理解正确,编译器就会抱怨,因为它无法弄清楚线程(以及因此关闭的值)不应超出此函数的结尾。我需要以某种方式告诉它,可能是通过指定生命周期?我不知道将生命周期说明符放在什么上,并且似乎没有某种特殊的'this_function生命周期,所以我不确定(标记){{1}并且具有相同生命周期的每个人handles都可以解决问题。

以下是产生错误消息的代码。

handle

0 个答案:

没有答案