我必须为我的国际象棋引擎实现UCI协议。
它需要从命令行读取命令。发送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
}
这段代码不起作用,因为我假设该字段刚刚被复制。但是,我尝试提供结构,然后代码抱怨,因为您无法同时拥有可变引用和普通引用。我还研究了与线程通信的方法。然而,大多数解决方案都使用通道来实现这一点,但我认为通道在我的情况下不起作用,因为线程总是在计算,所以它只会在终止后接收通道的命令。
你需要使用一个特殊的引用,一个 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
}
}
}