I am currently trying to find a way of making a separate thread listen for UCI-commands on stdin while searching, but I can't quite figure it out...
How do you do this?
I have tried having a global structure object that holds a "stop" and a "quit" flag, that gets set to true when we're told to stop or quit respectively. Then, before launching the search, I start a thread that runs the following function that listens for UCI input:
Code: Select all
void COMM::listen_for_input(bool stopset, long long stoptime) {
    std::string input = "";
    while (!stopset || getTimeMs() < stoptime) {
        input = "";
        std::getline(std::cin, input);
        if (input.find("quit") != std::string::npos) {
            ld.quit = true;
            return;
        }
    
        if (input.find("stop") != std::string::npos) {
            ld.stop = true;
            return;
        }
        if ((stopset && getTimeMs() >= stoptime) || Search::isStop.load(std::memory_order_relaxed)) {
            return;
        }
    }
}
Code: Select all
void COMM::check_stopped_search(SearchThread_t* ss) {
	// Step 1. Check for timed out search
	if (ss->info->timeset && getTimeMs() >= ss->info->stoptime) {
		ss->info->stopped = true;
	}
	// Step 2. Check for input from the GUI telling us to quit or stop. Only do this if we're the main thread.
	if (ss->thread_id == 0) {
		// Step 2A. Check for stop command
		if (ld.stop) {
			ss->info->stopped = true;
		}
		// Step 2B. Check for quit command
		if (ld.quit) {
			ss->info->quit = true;
			ss->info->stopped = true;
		}
		// Step 2C. If we've been told to stop or quit, tell the other threads with the std::atomic<bool> isStop flag
		if (ss->info->stopped) {
			Search::isStop = true;
		}
	}
	// Step 3. If we're not the main thread, check to see if we've been told to stop.
	else {
		if (Search::isStop.load(std::memory_order_relaxed)) {
			ss->info->stopped = true;
		}
	}
}
My problem is that when I try to join the listener thread, it only does so if it specifically recieves a "stop" or "quit" command (but the engine doesn't even crash; it just sits there doing nothing). It doesn't stop automatically even though I make it return if it times out... And I really can't see what the problem is
Can anyone see the error? How is this input-listening usually done when searching?
Thanks in advance
