From bbafc09ef8fc5a0267cc33f42c032eeddb22ea28 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sun, 4 Oct 2020 17:33:47 +0800 Subject: [PATCH] =?UTF-8?q?thread:=20=E6=B7=BB=E5=8A=A0=20Thread::pause()?= =?UTF-8?q?=20=E5=87=BD=E6=95=B0=E5=B9=B6=E5=B0=86=20stop=20=E6=9B=B4?= =?UTF-8?q?=E5=90=8D=E4=B8=BA=20pause=20(WIP)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 尚未确认是否正常工作。 --- src/thread.cpp | 7 +++++++ src/thread.h | 2 ++ src/ui/qt/gamecontroller.cpp | 8 ++++---- src/ui/qt/gamecontroller.h | 14 +++++++------- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/thread.cpp b/src/thread.cpp index aa25408a..193f6e1e 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -104,6 +104,13 @@ void Thread::start_searching() cv.notify_one(); // Wake up the thread in idle_loop() } +void Thread::pause() +{ + // TODO: Can work? + std::lock_guard lk(mutex); + searching = false; + cv.notify_one(); // Wake up the thread in idle_loop() +} /// Thread::wait_for_search_finished() blocks on the condition variable /// until the thread has finished searching. diff --git a/src/thread.h b/src/thread.h index 5f7e5a16..9fcbe7a7 100644 --- a/src/thread.h +++ b/src/thread.h @@ -61,6 +61,8 @@ public: void wait_for_search_finished(); int best_move_count(Move move) const; + void pause(); + size_t pvIdx, pvLast; uint64_t ttHitAverage; int selDepth, nmpMinPly; diff --git a/src/ui/qt/gamecontroller.cpp b/src/ui/qt/gamecontroller.cpp index 4685b72a..21b25c5c 100644 --- a/src/ui/qt/gamecontroller.cpp +++ b/src/ui/qt/gamecontroller.cpp @@ -116,7 +116,7 @@ GameController::~GameController() killTimer(timeID); // 停掉线程 - stopAndWaitThreads(); + pauseAndWaitThreads(); deleteAiThreads(); #ifdef ENDGAME_LEARNING @@ -194,7 +194,7 @@ void GameController::gameReset() // 停掉线程 if (!gameOptions.getAutoRestart()) { - stopThreads(); + pauseThreads(); resetAiPlayers(); } @@ -877,7 +877,7 @@ bool GameController::actionPiece(QPointF pos) } // 如果已经决出胜负 else { - stopThreads(); + pauseThreads(); } } @@ -1017,7 +1017,7 @@ bool GameController::command(const string &cmd, bool update /* = true */) } // 如果已经决出胜负 else { - stopThreads(); + pauseThreads(); gameEndTime = now(); gameDurationTime = gameEndTime - gameStartTime; diff --git a/src/ui/qt/gamecontroller.h b/src/ui/qt/gamecontroller.h index 9152a9c7..c20d680f 100644 --- a/src/ui/qt/gamecontroller.h +++ b/src/ui/qt/gamecontroller.h @@ -279,19 +279,19 @@ public slots: void stopAndWaitAiThreads() { if (isAiPlayer[BLACK]) { - aiThread[BLACK]->start_searching(); + aiThread[BLACK]->pause(); aiThread[BLACK]->wait_for_search_finished(); } if (isAiPlayer[WHITE]) { - aiThread[WHITE]->start_searching(); + aiThread[WHITE]->pause(); aiThread[WHITE]->wait_for_search_finished(); } } - void stopThreads() + void pauseThreads() { - aiThread[BLACK]->start_searching(); - aiThread[WHITE]->start_searching(); + aiThread[BLACK]->pause(); + aiThread[WHITE]->pause(); } void waitThreads() @@ -300,9 +300,9 @@ public slots: aiThread[WHITE]->wait_for_search_finished(); } - void stopAndWaitThreads() + void pauseAndWaitThreads() { - stopThreads(); + pauseThreads(); waitThreads(); }