2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
5 Stockfish is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Stockfish is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef THREAD_H_INCLUDED
20 #define THREAD_H_INCLUDED
23 #include <condition_variable>
33 #include "thread_win32_osx.h"
37 /// Thread class keeps together all the thread-related stuff. We use
38 /// per-thread pawn and material hash tables so that once we get a
39 /// pointer to an entry its life time is unlimited and we don't have
40 /// to care about someone changing the entry under our feet.
45 std::condition_variable cv;
47 bool exit = false, searching = true; // Set before starting std::thread
48 NativeThread stdThread;
51 explicit Thread(size_t);
53 virtual void search();
56 void start_searching();
57 void wait_for_search_finished();
58 size_t id() const { return idx; }
60 Pawns::Table pawnsTable;
61 Material::Table materialTable;
63 std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
64 int selDepth, nmpMinPly;
66 Value bestValue, optimism[COLOR_NB];
70 Search::RootMoves rootMoves;
71 Depth rootDepth, completedDepth;
73 CounterMoveHistory counterMoves;
74 ButterflyHistory mainHistory;
75 CapturePieceToHistory captureHistory;
76 ContinuationHistory continuationHistory[2][2];
80 /// MainThread is a derived class specific for main thread
82 struct MainThread : public Thread {
86 void search() override;
90 double previousTimeReduction;
91 Value bestPreviousScore;
92 Value bestPreviousAverageScore;
96 std::atomic_bool ponder;
100 /// ThreadPool struct handles all the threads-related stuff like init, starting,
101 /// parking and, most importantly, launching a thread. All the access to threads
102 /// is done through this class.
106 void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
110 MainThread* main() const { return static_cast<MainThread*>(threads.front()); }
111 uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
112 uint64_t tb_hits() const { return accumulate(&Thread::tbHits); }
113 Thread* get_best_thread() const;
114 void start_searching();
115 void wait_for_search_finished() const;
117 std::atomic_bool stop, increaseDepth;
119 auto cbegin() const noexcept { return threads.cbegin(); }
120 auto begin() noexcept { return threads.begin(); }
121 auto end() noexcept { return threads.end(); }
122 auto cend() const noexcept { return threads.cend(); }
123 auto size() const noexcept { return threads.size(); }
124 auto empty() const noexcept { return threads.empty(); }
127 StateListPtr setupStates;
128 std::vector<Thread*> threads;
130 uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const {
133 for (Thread* th : threads)
134 sum += (th->*member).load(std::memory_order_relaxed);
139 extern ThreadPool Threads;
141 } // namespace Stockfish
143 #endif // #ifndef THREAD_H_INCLUDED