From ad937722c487b9de5fe28cba1f1449b1ea4d1611 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Thu, 19 Dec 2024 14:09:53 -0500 Subject: [PATCH] stub: stdin gets its own thread --- src/main.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 31a6da2..586f99d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ Copyright © 2024 dogeystamp use chess_inator::prelude::*; use std::cmp::min; use std::io; -use std::sync::mpsc::channel; +use std::sync::mpsc::{channel, Sender}; use std::thread; use std::time::Duration; @@ -301,15 +301,31 @@ fn cmd_eval(mut _tokens: std::str::SplitWhitespace<'_>, board: &mut Board) { println!("STATIC EVAL (negative black, positive white):\n- pst: {}\n- king distance: {} ({} distance)\n- phase: {}\n- total: {}", res.pst_eval, res.king_distance_eval, res.king_distance, res.phase, res.total_eval); } -fn main() { - let stdin = io::stdin(); +/// Read stdin line-by-line in a non-blocking way (in another thread) +/// +/// # Arguments +/// - `tx`: channel write end to send lines to +fn task_stdin_reader(tx: Sender) { + thread::spawn(move || { + let stdin = io::stdin(); + loop { + let mut line = String::new(); + stdin.read_line(&mut line).unwrap(); + tx.send(line).unwrap(); + } + }); +} + +fn main() { let mut board = Board::starting_pos(); let mut transposition_table = TranspositionTable::new(24); + let (tx, rx) = channel(); + task_stdin_reader(tx.clone()); + loop { - let mut line = String::new(); - stdin.read_line(&mut line).unwrap(); + let line = rx.recv().unwrap(); let mut tokens = line.split_whitespace(); while let Some(token) = tokens.next() { match token {