2
struct Level{
    i_vec: ~[int]
}
pub struct GameManager{
    lvl: Level
}
impl GameManager {
    pub fn new() -> GameManager{
        GameManager {lvl: Level{i_vec: ~[]}}
    }
    pub fn new_game(f: ~fn()) {
        do spawn {
            f();
        }
    }
    pub fn default_game_loop(lvl: &Level ,f: &fn() ){
        loop {
            f();
            break;
        }
    }
}
fn main() {
    let mut gm = GameManager::new();
    do GameManager::new_game(){
        // I know I could move "gm" here, but I would like
        // to know how to capture mutable variables.
        do GameManager::default_game_loop(&gm.lvl){

        }
    }

}
/*
/home/maik/source/test.rs:28:43: 28:45 error: mutable variables cannot be implicitly captured
/home/maik/source/test.rs:28         do GameManager::default_game_loop(&gm.lvl){
                                                                        ^~
error: aborting due to previous error
[Finished in 0.2s with exit code 101]
*/

如何捕获可变变量?

我也尝试让这些函数成为方法,但后来一切都崩溃了,因为它试图将自己移动到闭包中

do gm.default_game_loop(){
    let level = &gm.lvl;    
}

有我可以使用的 self 参数吗?因为 gm 本身应该在闭包中可用

do gm.default_game_loop(){
        let level = self.lvl;    
}
4

1 回答 1

1

您遇到的问题是您不能直接在两个任务之间共享内存。处理这个问题的传统方法是将数据移动到线程。这是一个例子:

use std::task;

struct Level {
    i_vec: ~[int]
}
pub struct GameManager {
    lvl: Level
}
impl GameManager {
    pub fn new() -> GameManager {
        GameManager { lvl: Level { i_vec: ~[] } }
    }
    pub fn new_game<A: Send>(a: A, f: ~fn(A)) {
        task::spawn_with(a, f)
    }
    pub fn default_game_loop(lvl: &Level, f: &fn()) {
        loop {
            f();
            break;
        }
    }
}
fn main() {
    let mut gm = GameManager::new();
    do GameManager::new_game(gm) |gm| {
        // I know I could move "gm" here, but I would like
        // to know how to capture mutable variables.
        do GameManager::default_game_loop(&gm.lvl) {

        }
    }
}

如果您需要以某种方式在多个线程之间共享可变状态,那就有点棘手了。为此,您可以设置一个拥有可变状态的任务,并通过std::comm::stream通道或通过互斥保护的 extra::arc::RWArc 完成修改

于 2013-08-20T03:28:09.170 回答