- // Threads will sent to sleep as soon as created, only main thread is kept alive
- activeThreads = 1;
- threads[0].state = Thread::SEARCHING;
+ for (int i = 0; i < MAX_THREADS; i++)
+ if (i < activeThreads)
+ {
+ // Dynamically allocate pawn and material hash tables according to the
+ // number of active threads. This avoids preallocating memory for all
+ // possible threads if only few are used as, for instance, on mobile
+ // devices where memory is scarce and allocating for MAX_THREADS could
+ // even result in a crash.
+ threads[i].pawnTable.init();
+ threads[i].materialTable.init();
+
+ threads[i].do_sleep = false;
+ }
+ else
+ threads[i].do_sleep = true;
+}
+
+
+// init() is called during startup. Initializes locks and condition variables
+// and launches all threads sending them immediately to sleep.