如何将指向可变变量的指针共享给线程? [重复]

问题描述 投票:1回答:1

我必须为我的国际象棋引擎实施UCI protocol

它需要从命令行读取命令。发送go命令时,必须启动搜索。但是,在此搜索期间,仍然必须接收其他命令,例如stop。在stop的情况下,搜索必须完全退出。

这就是代码的外观(省略了非重要的细节)。

pub fn main() {
    let mut stop: bool = false;
    loop {
        line.clear();
        stdin.read_line(&mut line).ok().unwrap();
        let arg: Vec<&str> = line.split_whitespace().collect();
        let cmd = arg[0];
        match cmd.trim() {
            "" => continue,
            "go" => {
                stop = false;
                thread::spawn(move || start_search(&stop, GameState, History, Timecontrol));
            }
            "stop" => {
                stop = true;
                thread::sleep(Duration::from_millis(50));
            }
            "quit" => {
                stop = true;
                thread::sleep(Duration::from_millis(50));
                break;
            }
            _ => {
                println!("Unknown command {}", line);
            }
        }
    }
}

pub fn start_search(stop_reference: &bool, _: GameState, _: History, _: Timecontrol) {
    /* Do search stuff here...
     */
    //Has to break when stop_reference is set to true
}

此代码不起作用,因为我假设该字段只是被复制。但是我尝试给出结构然后代码抱怨因为你不能同时拥有一个可变引用和一个普通引用。我还研究了与线程进行通信的方法。然而,大多数解决方案使用一个通道来实现这一点,但我认为一个通道在我的情况下不起作用,因为线程总是在计算,所以它只会在它终止之后接收通道的命令。

rust thread-safety uci
1个回答
3
投票

你需要使用一个特殊的引用,一个Arc来共享线程之间的布尔值:

pub fn main() {
    let stop = Arc::new(AtomicBool::new(false));
    loop {
        //...

        let stop = Arc::clone(&stop);
        thread::spawn(move || {
            start_search(stop);
        })
    }
}
pub fn start_search(stop: Arc<AtomicBool>) {
    loop {
        if stop.load(Ordering::Relaxed) {
            // STOP
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.