-namespace { extern "C" {
-
- // start_routine() is the C function which is called when a new thread
- // is launched. It is a wrapper to member function pointed by start_fn.
-
- long start_routine(Thread* th) { (th->*(th->start_fn))(); return 0; }
-
-} }
-
-
-// Thread c'tor starts a newly-created thread of execution that will call
-// the idle loop function pointed by start_fn going immediately to sleep.
-
-Thread::Thread(Fn fn) : splitPoints() {
-
- is_searching = do_exit = false;
- maxPly = splitPointsCnt = 0;
- curSplitPoint = NULL;
- start_fn = fn;
- idx = Threads.size();
-
- do_sleep = (fn != &Thread::main_loop); // Avoid a race with start_searching()
-
- if (!thread_create(handle, start_routine, this))
- {
- std::cerr << "Failed to create thread number " << idx << std::endl;
- ::exit(EXIT_FAILURE);
- }
-}
-
-
-// Thread d'tor waits for thread termination before to return.
-
-Thread::~Thread() {
-
- assert(do_sleep);
-
- do_exit = true; // Search must be already finished
- notify_one();
- thread_join(handle); // Wait for thread termination
-}
-
-
-// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
-// then calls check_time(). If maxPly is 0 thread sleeps until is woken up.