+// Thread c'tor creates and launches the OS thread, that will go immediately to
+// sleep.
+
+Thread::Thread(int id) {
+
+ is_searching = do_exit = false;
+ maxPly = splitPointsCnt = 0;
+ curSplitPoint = NULL;
+ threadID = id;
+ do_sleep = (id != 0); // Avoid a race with start_thinking()
+
+ lock_init(sleepLock);
+ cond_init(sleepCond);
+
+ for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++)
+ lock_init(splitPoints[j].lock);
+
+ if (!thread_create(handle, start_routine, this))
+ {
+ std::cerr << "Failed to create thread number " << id << std::endl;
+ ::exit(EXIT_FAILURE);
+ }
+}
+
+
+// Thread d'tor will wait for thread termination before to return.
+
+Thread::~Thread() {
+
+ assert(do_sleep);
+
+ do_exit = true; // Search must be already finished
+ wake_up();
+
+ thread_join(handle); // Wait for thread termination
+
+ lock_destroy(sleepLock);
+ cond_destroy(sleepCond);
+
+ for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++)
+ lock_destroy(splitPoints[j].lock);
+}
+
+