+// ThreadPool struct handles all the threads-related stuff like init, starting,
+// parking and, most importantly, launching a thread. All the access to threads
+// is done through this class.
+class ThreadPool {
+ public:
+ ThreadPool() {}
+
+ ~ThreadPool() {
+ // destroy any existing thread(s)
+ if (threads.size() > 0)
+ {
+ main_thread()->wait_for_search_finished();
+
+ threads.clear();
+ }
+ }
+
+ ThreadPool(const ThreadPool&) = delete;
+ ThreadPool(ThreadPool&&) = delete;
+
+ ThreadPool& operator=(const ThreadPool&) = delete;
+ ThreadPool& operator=(ThreadPool&&) = delete;
+
+ void start_thinking(const OptionsMap&, Position&, StateListPtr&, Search::LimitsType);
+ void run_on_thread(size_t threadId, std::function<void()> f);
+ void wait_on_thread(size_t threadId);
+ size_t num_threads() const;
+ void clear();
+ void set(const NumaConfig& numaConfig,
+ Search::SharedState,
+ const Search::SearchManager::UpdateContext&);
+
+ Search::SearchManager* main_manager();
+ Thread* main_thread() const { return threads.front().get(); }
+ uint64_t nodes_searched() const;
+ uint64_t tb_hits() const;
+ Thread* get_best_thread() const;
+ void start_searching();
+ void wait_for_search_finished() const;
+
+ std::vector<size_t> get_bound_thread_count_by_numa_node() const;
+
+ void ensure_network_replicated();
+
+ std::atomic_bool stop, abortedSearch, increaseDepth;
+
+ auto cbegin() const noexcept { return threads.cbegin(); }
+ auto begin() noexcept { return threads.begin(); }
+ auto end() noexcept { return threads.end(); }
+ auto cend() const noexcept { return threads.cend(); }
+ auto size() const noexcept { return threads.size(); }
+ auto empty() const noexcept { return threads.empty(); }
+
+ private:
+ StateListPtr setupStates;
+ std::vector<std::unique_ptr<Thread>> threads;
+ std::vector<NumaIndex> boundThreadToNumaNode;
+
+ uint64_t accumulate(std::atomic<uint64_t> Search::Worker::*member) const {
+
+ uint64_t sum = 0;
+ for (auto&& th : threads)
+ sum += (th->worker.get()->*member).load(std::memory_order_relaxed);
+ return sum;
+ }