From f2ad307de313d18c56b147f8a682971cd8ca088a Mon Sep 17 00:00:00 2001 From: Tomasz Sobczyk Date: Tue, 20 Oct 2020 10:50:59 +0200 Subject: [PATCH] Clarify the behaviour of execute_with_worker[s] --- src/thread.cpp | 3 +-- src/thread.h | 11 ++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/thread.cpp b/src/thread.cpp index 72333078..e867048d 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -186,8 +186,7 @@ void ThreadPool::clear() { main()->previousTimeReduction = 1.0; } - -void ThreadPool::execute_with_workers(std::function worker) +void ThreadPool::execute_with_workers(const std::function& worker) { for(Thread* th : *this) { diff --git a/src/thread.h b/src/thread.h index 7474ea44..c0a01770 100644 --- a/src/thread.h +++ b/src/thread.h @@ -52,7 +52,13 @@ public: explicit Thread(size_t); virtual ~Thread(); virtual void search(); + + // The function object to be executed is taken by value to remove + // the need for separate lvalue and rvalue overloads. + // The worker thread needs to have ownership of the task + // to be executed because otherwise there's no way to manage its lifetime. virtual void execute_with_worker(std::function t); + void clear(); void idle_loop(); void start_searching(); @@ -109,7 +115,10 @@ struct MainThread : public Thread { struct ThreadPool : public std::vector { - void execute_with_workers(std::function worker); + // Each thread gets its own copy of the `worker` function object. + // This means that each worker thread will have exclusive access + // to the state of the `worker` function object. + void execute_with_workers(const std::function& worker); void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false); void clear();