// when start_routine (and hence virtual idle_loop) is called and when joining.
template<typename T> T* new_thread() {
- T* th = new T();
- th->nativeThread = std::thread(&ThreadBase::idle_loop, th); // Will go to sleep
- return th;
+ std::thread* th = new T;
+ *th = std::thread(&T::idle_loop, (T*)th); // Will go to sleep
+ return (T*)th;
}
void delete_thread(ThreadBase* th) {
th->mutex.unlock();
th->notify_one();
- th->nativeThread.join(); // Wait for thread termination
+ th->join(); // Wait for thread termination
delete th;
}
void ThreadBase::notify_one() {
- std::unique_lock<Mutex>(this->mutex);
+ std::unique_lock<Mutex> lk(mutex);
sleepCondition.notify_one();
}
SplitPoint& sp = splitPoints[splitPointsSize];
sp.spinlock.acquire(); // No contention here until we don't increment splitPointsSize
+ spinlock.acquire();
sp.master = this;
sp.parentSplitPoint = activeSplitPoint;
++splitPointsSize;
activeSplitPoint = &sp;
activePosition = nullptr;
+ spinlock.release();
// Try to allocate available threads
Thread* slave;
Thread::idle_loop(); // Force a call to base class idle_loop()
+ sp.spinlock.acquire();
+ spinlock.acquire();
+
// In the helpful master concept, a master can help only a sub-tree of its
// split point and because everything is finished here, it's not possible
// for the master to be booked.
// We have returned from the idle loop, which means that all threads are
// finished. Note that decreasing splitPointsSize must be done under lock
// protection to avoid a race with Thread::can_join().
- sp.spinlock.acquire();
-
--splitPointsSize;
activeSplitPoint = sp.parentSplitPoint;
activePosition = &pos;
*bestMove = sp.bestMove;
*bestValue = sp.bestValue;
+ spinlock.release();
sp.spinlock.release();
}
void ThreadPool::exit() {
delete_thread(timer); // As first because check_time() accesses threads data
+ timer = nullptr;
for (Thread* th : *this)
delete_thread(th);
+
+ clear(); // Get rid of stale pointers
}
assert(requested > 0);
- // If zero (default) then set best minimum split depth automatically
- if (!minimumSplitDepth)
- minimumSplitDepth = requested < 8 ? 4 * ONE_PLY : 7 * ONE_PLY;
-
while (size() < requested)
push_back(new_thread<Thread>());
StateStackPtr& states) {
main()->join();
- SearchTime = now(); // As early as possible
-
Signals.stopOnPonderhit = Signals.firstRootMove = false;
Signals.stop = Signals.failedLowAtRoot = false;