Prefer sub-classing to composition in this case.
No functional change.
// Reset the threads, still sleeping: will be wake up at split time
for (size_t i = 0; i < Threads.size(); i++)
- Threads[i].maxPly = 0;
+ Threads[i]->maxPly = 0;
Threads.sleepWhileIdle = Options["Use Sleeping Threads"];
// Set best timer interval to avoid lagging under time pressure. Timer is
// used to check for remaining available thinking time.
- Threads.timer_thread()->msec =
+ Threads.timer->msec =
Limits.use_time_management() ? std::min(100, std::max(TimeMgr.available_time() / 16, TimerResolution)) :
Limits.nodes ? 2 * TimerResolution
: 100;
- Threads.timer_thread()->notify_one(); // Wake up the recurring timer
+ Threads.timer->notify_one(); // Wake up the recurring timer
id_loop(RootPos); // Let's start searching !
- Threads.timer_thread()->msec = 0; // Stop the timer
+ Threads.timer->msec = 0; // Stop the timer
Threads.sleepWhileIdle = true; // Send idle threads to sleep
if (Options["Use Search Log"])
int selDepth = 0;
for (size_t i = 0; i < Threads.size(); i++)
- if (Threads[i].maxPly > selDepth)
- selDepth = Threads[i].maxPly;
+ if (Threads[i]->maxPly > selDepth)
+ selDepth = Threads[i]->maxPly;
for (size_t i = 0; i < uciPVSize; i++)
{
// Loop across all split points and sum accumulated SplitPoint nodes plus
// all the currently active slaves positions.
for (size_t i = 0; i < Threads.size(); i++)
- for (int j = 0; j < Threads[i].splitPointsSize; j++)
+ for (int j = 0; j < Threads[i]->splitPointsSize; j++)
{
- SplitPoint& sp = Threads[i].splitPoints[j];
+ SplitPoint& sp = Threads[i]->splitPoints[j];
sp.mutex.lock();
sleepWhileIdle = true;
timer = new TimerThread();
- threads.push_back(new MainThread());
+ push_back(new MainThread());
read_uci_options();
}
delete timer; // As first because check_time() accesses threads data
- for (size_t i = 0; i < threads.size(); i++)
- delete threads[i];
+ for (iterator it = begin(); it != end(); ++it)
+ delete *it;
}
assert(requested > 0);
- while (threads.size() < requested)
- threads.push_back(new Thread());
+ while (size() < requested)
+ push_back(new Thread());
- while (threads.size() > requested)
+ while (size() > requested)
{
- delete threads.back();
- threads.pop_back();
+ delete back();
+ pop_back();
}
}
bool ThreadPool::slave_available(Thread* master) const {
- for (size_t i = 0; i < threads.size(); i++)
- if (threads[i]->is_available_to(master))
+ for (const_iterator it = begin(); it != end(); ++it)
+ if ((*it)->is_available_to(master))
return true;
return false;
splitPointsSize++;
activeSplitPoint = &sp;
- size_t slavesCnt = 1; // Master is always included
+ size_t slavesCnt = 1; // This thread is always included
- for (size_t i = 0; i < Threads.size() && !Fake; ++i)
- if (Threads[i].is_available_to(this) && ++slavesCnt <= Threads.maxThreadsPerSplitPoint)
+ for (ThreadPool::iterator it = Threads.begin(); it != Threads.end() && !Fake; ++it)
+ {
+ Thread* slave = *it;
+
+ if (slave->is_available_to(this) && ++slavesCnt <= Threads.maxThreadsPerSplitPoint)
{
- sp.slavesMask |= 1ULL << Threads[i].idx;
- Threads[i].activeSplitPoint = &sp;
- Threads[i].searching = true; // Slave leaves idle_loop()
- Threads[i].notify_one(); // Could be sleeping
+ sp.slavesMask |= 1ULL << slave->idx;
+ slave->activeSplitPoint = &sp;
+ slave->searching = true; // Slave leaves idle_loop()
+ slave->notify_one(); // Could be sleeping
}
+ }
sp.mutex.unlock();
Threads.mutex.unlock();
};
-/// ThreadPool class handles all the threads related stuff like init, starting,
+/// ThreadPool struct handles all the threads related stuff like init, starting,
/// parking and, the most important, launching a slave thread at a split point.
/// All the access to shared thread data is done through this class.
-class ThreadPool {
+struct ThreadPool : public std::vector<Thread*> {
-public:
void init(); // No c'tor and d'tor, threads rely on globals that should
void exit(); // be initialized and valid during the whole thread lifetime.
- Thread& operator[](size_t id) { return *threads[id]; }
- size_t size() const { return threads.size(); }
- MainThread* main_thread() { return static_cast<MainThread*>(threads[0]); }
- TimerThread* timer_thread() { return timer; }
-
+ MainThread* main_thread() { return static_cast<MainThread*>((*this)[0]); }
void read_uci_options();
bool slave_available(Thread* master) const;
void wait_for_think_finished();
size_t maxThreadsPerSplitPoint;
Mutex mutex;
ConditionVariable sleepCondition;
-
-private:
- std::vector<Thread*> threads;
TimerThread* timer;
};