From cda006ed4be39839d0d45833c4c8b9724c63b7a8 Mon Sep 17 00:00:00 2001 From: rn5f107s2 Date: Sat, 2 Nov 2024 09:53:07 +0100 Subject: [PATCH] Probably fix crashes by not doing some weird thread detachery and instead join them bench 8493466 --- src/UCI.cpp | 12 ++++++++++-- src/search.cpp | 4 +--- src/thread.cpp | 10 ++++------ src/thread.h | 1 - 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/UCI.cpp b/src/UCI.cpp index df45723..217b868 100644 --- a/src/UCI.cpp +++ b/src/UCI.cpp @@ -48,6 +48,9 @@ void UCI::uci([[maybe_unused]] const std::string &args) { } void UCI::isready([[maybe_unused]] const std::string &args) { + if (threads.done()) + threads.join(); + std::cout << "readyok" << std::endl; } @@ -207,10 +210,15 @@ void UCI::start(int argc, char** argv) { std::string in = argv[i]; if (in == "quit") - return; + break; handleInput(in); + + // If a search was started, wait for it to finish + while (!threads.done()); } + + threads.join(); } void UCI::loop() { @@ -220,7 +228,7 @@ void UCI::loop() { if (input == "quit") { threads.stop(); - while (!threads.done()) {} + threads.join(); return; } diff --git a/src/search.cpp b/src/search.cpp index a7590c4..e5eddf8 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -40,9 +40,7 @@ std::string SearchState::outputWDL(Position &pos) { } int SearchState::startSearch(Position &pos, SearchTime &st, int maxDepth, Move &bestMove) { - int score = iterativeDeepening(pos, st, maxDepth, bestMove); - thread->detach(); - return score; + return iterativeDeepening(pos, st, maxDepth, bestMove); } void SearchState::clearHistory() { diff --git a/src/thread.cpp b/src/thread.cpp index 190584b..d3b18a2 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -23,12 +23,6 @@ void Thread::initSearchInfo(SearchTime st) { state.si.st = st; } -void Thread::detach() { - while (!thread.joinable()) {} - - thread.detach(); -} - int Thread::id() const { return threadId; } @@ -42,6 +36,10 @@ u64 Thread::nodes() { } void ThreadPool::start(Position &pos, SearchTime &st, int depth) { + // Threads should already be joined if isready was sent after the last search ended, however in order to not + // rely on that, also join threads here + join(); + // Before starting the threads set all of them to searching, to avoid the mainthread stopping search // while other threads havent been started yet, also reset SearchInfo here, to avoid reading old nodecounts for (auto &t : threads) { diff --git a/src/thread.h b/src/thread.h index 987ebb7..37e9014 100644 --- a/src/thread.h +++ b/src/thread.h @@ -24,7 +24,6 @@ class Thread { void stop(); void clear(); void initSearchInfo(SearchTime st); - void detach(); bool done(); int id() const; u64 nodes();