DWORD WINAPI start_routine(LPVOID threadID) {
- Threads.idle_loop(*(int*)threadID, NULL);
+ Threads[*(int*)threadID].idle_loop(NULL);
return 0;
}
void* start_routine(void* threadID) {
- Threads.idle_loop(*(int*)threadID, NULL);
+ Threads[*(int*)threadID].idle_loop(NULL);
return NULL;
}
maxThreadsPerSplitPoint = Options["Maximum Number of Threads per Split Point"].value<int>();
minimumSplitDepth = Options["Minimum Split Depth"].value<int>() * ONE_PLY;
useSleepingThreads = Options["Use Sleeping Threads"].value<bool>();
- activeThreads = Options["Threads"].value<int>();
+
+ set_size(Options["Threads"].value<int>());
+}
+
+
+// set_size() changes the number of active threads and raises do_sleep flag for
+// all the unused threads that will go immediately to sleep.
+
+void ThreadsManager::set_size(int cnt) {
+
+ assert(cnt > 0 && cnt <= MAX_THREADS);
+
+ activeThreads = cnt;
+
+ for (int i = 0; i < MAX_THREADS; i++)
+ threads[i].do_sleep = !(i < activeThreads);
}
void ThreadsManager::init() {
- int threadID[MAX_THREADS];
-
- // This flag is needed to properly end the threads when program exits
- allThreadsShouldExit = false;
-
- // Threads will sent to sleep as soon as created, only main thread is kept alive
- activeThreads = 1;
+ // Threads will go to sleep as soon as created, only main thread is kept alive
+ set_size(1);
threads[0].state = Thread::SEARCHING;
+ threads[0].threadID = 0;
// Allocate pawn and material hash tables for main thread
init_hash_tables();
// Create and startup all the threads but the main that is already running
for (int i = 1; i < MAX_THREADS; i++)
{
- threads[i].state = Thread::INITIALIZING;
- threadID[i] = i;
+ threads[i].state = Thread::AVAILABLE;
+ threads[i].threadID = i;
#if defined(_MSC_VER)
- bool ok = (CreateThread(NULL, 0, start_routine, (LPVOID)&threadID[i], 0, NULL) != NULL);
+ bool ok = (CreateThread(NULL, 0, start_routine, (LPVOID)&threads[i].threadID , 0, NULL) != NULL);
#else
pthread_t pthreadID;
- bool ok = (pthread_create(&pthreadID, NULL, start_routine, (void*)&threadID[i]) == 0);
+ bool ok = (pthread_create(&pthreadID, NULL, start_routine, (void*)&threads[i].threadID) == 0);
pthread_detach(pthreadID);
#endif
if (!ok)
std::cout << "Failed to create thread number " << i << std::endl;
::exit(EXIT_FAILURE);
}
-
- // Wait until the thread has finished launching and is gone to sleep
- while (threads[i].state == Thread::INITIALIZING) {}
}
}
void ThreadsManager::exit() {
- // Force the woken up threads to exit idle_loop() and hence terminate
- allThreadsShouldExit = true;
-
for (int i = 0; i < MAX_THREADS; i++)
{
- // Wake up all the threads and wait for termination
+ // Wake up all the slave threads and wait for termination
if (i != 0)
{
+ threads[i].do_terminate = true;
threads[i].wake_up();
while (threads[i].state != Thread::TERMINATED) {}
}
// Thread::WORKISWAITING. We send the split point as a second parameter to
// the idle loop, which means that the main thread will return from the idle
// loop when all threads have finished their work at this split point.
- idle_loop(master, sp);
+ masterThread.idle_loop(sp);
// In helpful master concept a master can help only a sub-tree, and
// because here is all finished is not possible master is booked.