]> git.sesse.net Git - stockfish/commitdiff
Avoid a race at thread creation
authorMarco Costalba <mcostalba@gmail.com>
Tue, 3 Jan 2012 20:13:29 +0000 (21:13 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Tue, 3 Jan 2012 20:31:50 +0000 (21:31 +0100)
Before creating main thread we set its do_sleep flag to true,
then thread is created and it will go to sleep in main_loop()
after resetting do_sleep.

But if after the setting of do_sleep and before its resetting
the UI thread calls start_thinking() it will not wait on:

  if (!asyncMode)
      while (!main.do_sleep)
          cond_wait(&sleepCond, &main.sleepLock);

as it should but will immediately return before the main thread has
started the search. This very rare race show itself during bench,
when the first position is erroneusly skipped so that bench node count
results of 5309038 instead of the correct 5457475.

The patch is somewhat tricky, but is simple and it works!

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/thread.cpp

index 394e06980c7f83f1b9b30e810b016bd726e4ce81..832648015cfd362cf9488cca93c663b005fc1af4 100644 (file)
@@ -172,7 +172,7 @@ void ThreadsManager::init() {
   for (int i = 0; i <= MAX_THREADS; i++)
   {
       threads[i].is_searching = false;
-      threads[i].do_sleep = true;
+      threads[i].do_sleep = (i != 0); // Avoid a race with start_thinking()
       threads[i].threadID = i;
 
 #if defined(_MSC_VER)