mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-20 00:56:39 +08:00
Use only one ConditionVariable to sync UI
To sync UI with main thread it is enough a single condition variable because here we have a single producer / single consumer design pattern. Two condition variables are strictly needed just for many producers / many consumers case. Note that this is possible because now we don't send to sleep idle threads anymore while searching, so that now only UI can wake up the main thread and we can use the same ConditionVariable for both threads. The natural consequence is to retire wait_for_think_finished() and move all the logic under MainThread class, yielding the rename of teh function to join() No functional change.
This commit is contained in:
@@ -251,7 +251,7 @@ void MainThread::idle_loop() {
|
||||
|
||||
while (!thinking && !exit)
|
||||
{
|
||||
Threads.sleepCondition.notify_one(); // Wake up the UI thread if needed
|
||||
sleepCondition.notify_one(); // Wake up the UI thread if needed
|
||||
sleepCondition.wait(lk);
|
||||
}
|
||||
|
||||
@@ -271,6 +271,15 @@ void MainThread::idle_loop() {
|
||||
}
|
||||
|
||||
|
||||
// MainThread::join() waits for main thread to finish the search
|
||||
|
||||
void MainThread::join() {
|
||||
|
||||
std::unique_lock<Mutex> lk(mutex);
|
||||
sleepCondition.wait(lk, [&]{ return !thinking; });
|
||||
}
|
||||
|
||||
|
||||
// ThreadPool::init() is called at startup to create and launch requested threads,
|
||||
// that will go immediately to sleep. We cannot use a c'tor because Threads is a
|
||||
// static object and we need a fully initialized engine at this point due to
|
||||
@@ -337,21 +346,12 @@ Thread* ThreadPool::available_slave(const SplitPoint* sp) const {
|
||||
}
|
||||
|
||||
|
||||
// ThreadPool::wait_for_think_finished() waits for main thread to finish the search
|
||||
|
||||
void ThreadPool::wait_for_think_finished() {
|
||||
|
||||
std::unique_lock<Mutex> lk(main()->mutex);
|
||||
sleepCondition.wait(lk, [&]{ return !main()->thinking; });
|
||||
}
|
||||
|
||||
|
||||
// ThreadPool::start_thinking() wakes up the main thread sleeping in
|
||||
// MainThread::idle_loop() and starts a new search, then returns immediately.
|
||||
|
||||
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
|
||||
StateStackPtr& states) {
|
||||
wait_for_think_finished();
|
||||
main()->join();
|
||||
|
||||
SearchTime = now(); // As early as possible
|
||||
|
||||
|
||||
Reference in New Issue
Block a user