Skip to content

Commit

Permalink
Probably fix crashes by not doing some weird thread detachery and ins…
Browse files Browse the repository at this point in the history
…tead join them

bench 8493466
  • Loading branch information
rn5f107s2 committed Nov 2, 2024
1 parent fa459c3 commit cda006e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/UCI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand All @@ -220,7 +228,7 @@ void UCI::loop() {

if (input == "quit") {
threads.stop();
while (!threads.done()) {}
threads.join();
return;
}

Expand Down
4 changes: 1 addition & 3 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 4 additions & 6 deletions src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Thread {
void stop();
void clear();
void initSearchInfo(SearchTime st);
void detach();
bool done();
int id() const;
u64 nodes();
Expand Down

0 comments on commit cda006e

Please sign in to comment.