Skip to content

Commit

Permalink
Avoid reading old nodecounts in multithreading
Browse files Browse the repository at this point in the history
Previously if the thread with id zero finished search before any of the other threads had reset their nodecount, the previous nodecount was read, this fixes this behaviour by clearing nodecounts of all threads before starting search

bench 5022919
  • Loading branch information
rn5f107s2 committed Jun 25, 2024
1 parent 8c3134d commit 2da3c1a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ void Thread::clear() {
state.clearHistory();
}

void Thread::initSearchInfo(SearchTime st) {
state.si.clear();
state.si.st = st;
}

void Thread::detach() {
thread.detach();
}
Expand All @@ -36,9 +41,11 @@ u64 Thread::nodes() {

void ThreadPool::start(Position &pos, SearchTime &st, int depth) {
// Before starting the threads set all of them to searching, to avoid the mainthread stopping search
// while other threads havent been started yet
for (auto &t : threads)
// while other threads havent been started yet, also reset SearchInfo here, to avoid reading old nodecounts
for (auto &t : threads) {
t.searching.store(true, std::memory_order_relaxed);
t.initSearchInfo(st);
}

for (auto &t : threads)
t.start(pos, st, depth);
Expand Down
1 change: 1 addition & 0 deletions src/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Thread {
void join();
void stop();
void clear();
void initSearchInfo(SearchTime st);
void detach();
bool done();
int id() const;
Expand Down

0 comments on commit 2da3c1a

Please sign in to comment.