2015-01-26 1 views
2

나는 Rust의 동시성을 사용하여 "Tower of Hanoi"게임을 구현하려고합니다. 솔직히, 나는 마지막 시간에 Rust의 일 평생을 이해하려고 노력했지만 아직 끝나지 않았습니다. 그래서 내가 이해하지 못하는 이상한 평생 실수를하게됩니다. 첫째, 여기에 코드스레딩 수명 오류

fn move_plate<'a>(stack_a: &'a mut Vec<i32>, stack_b: &'a mut Vec<i32>, 
    stack_c: &'a mut Vec<i32>, moves: &'a mut Vec<(i32, i32)>) 
{ 
     let mut moves1: Vec<(i32, i32)> = Vec::new(); 
     let guard1 = Thread::scoped(
      move || { move_plate(stack_a, stack_c, stack_b, (1, 3, 2), &mut moves1); 
     }); 
     guard1.join().ok(); 
} 

을의 중요한 부분입니다 그리고 여기 내가 이동에 다른 참조가 될 것이기 때문에, 기능을보다 오래 할 스레드를 방지 할 필요가 있음을 이해

error: cannot infer an appropriate lifetime due to conflicting requirements 
    let guard1 = Thread::scoped(move || { 
        move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1); 
       }); 
note: first, the lifetime cannot outlive the expression at 93:25... 
      let guard1 = Thread::scoped(move || { 

note: ...so that the declared lifetime parameter bounds are satisfied 
      let guard1 = Thread::scoped(move || { 

note: but, the lifetime must be valid for the expression at 93:45... 
      let guard1 = Thread::scoped(move || { 
       move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1); 
      }); 
note: ...so type `closure[]` of expression is valid during the expression 
      let guard1 = Thread::scoped(move || { 
       move_plate(height - 1, stack_a, stack_c, stack_b, (1, 3, 2), threads, depth + 1, &mut moves1); 
      }); 
error: declared lifetime bound not satisfied 
      let guard1 = Thread::scoped(move || { 

오류를하다 지나간. 하지만 스레드에 가입 했으므로 괜찮을 것입니다. 안 그래야합니까? 그 시점에서 나는 무엇을 놓치고 있습니까? 누군가가 나를 도울 수 있다면 정말 좋을 것이다. 나는 그 멋진 (그러나 복잡한) 종류의 물건에 익숙해지고있다.

답변

6

이것은 녹 시스템의 알려진 제한 사항이다. 현재 Rust는이 데이터가 Send 바운드를 충족시키고 Send'static을 의미하는 경우에만 스레드간에 데이터를 보낼 수 있습니다. 즉, 스레드 경계를 넘어 보낼 수있는 유일한 참조는 'static 것입니다.

an RFC이 부분적으로이 제한을 해제하여 비 'static 참조가 여러 작업간에 전송 될 수 있도록합니다. 나는 그것이 이미 받아 들여 졌다고 생각했으나 그것이 이상하지 않다. 그런 것을 지원하는 API는 이미 만들어졌지만 (이것은 혼란 스러울 수 있습니다), 언어는 아직 조정되지 않았습니다.