]> git.sesse.net Git - stockfish/blobdiff - src/thread.cpp
Use only one ConditionVariable to sync UI
[stockfish] / src / thread.cpp
index 4b5d845bd83577992601f1306a881bdacb812d19..a715d44ee17b2e5844e4be101b246041e9bc9c7d 100644 (file)
@@ -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
 
@@ -373,7 +373,5 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
           RootMoves.push_back(RootMove(m));
 
   main()->thinking = true;
-
-  for (Thread* th : *this)
-      th->notify_one();
+  main()->notify_one(); // Wake up main thread: 'thinking' must be already set
 }