X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fthread.cpp;h=a8e26b9f85c43a69a3f0260db1313285ca3ccee2;hb=86b95f210508de4c30fb5ee9f86efee6641d45f8;hp=5c10eee0635e8ddbf531d3e84577629aa74b0c01;hpb=ba85c59d96d962dddaa0f1a2608ebea2e8ae694b;p=stockfish diff --git a/src/thread.cpp b/src/thread.cpp index 5c10eee0..a8e26b9f 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -135,7 +135,7 @@ void ThreadsManager::set_size(int cnt) { void ThreadsManager::init() { - // Threads will sent to sleep as soon as created, only main thread is kept alive + // 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; @@ -159,24 +159,21 @@ void ThreadsManager::init() { // 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; + threads[i].state = Thread::AVAILABLE; threads[i].threadID = i; #if defined(_MSC_VER) - bool ok = (CreateThread(NULL, 0, start_routine, (LPVOID)&threads[i].threadID , 0, NULL) != NULL); + threads[i].handle = CreateThread(NULL, 0, start_routine, (LPVOID)&threads[i].threadID, 0, NULL); + bool ok = (threads[i].handle != NULL); #else - pthread_t pthreadID; - bool ok = (pthread_create(&pthreadID, NULL, start_routine, (void*)&threads[i].threadID) == 0); - pthread_detach(pthreadID); + bool ok = (pthread_create(&threads[i].handle, NULL, start_routine, (void*)&threads[i].threadID) == 0); #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) {} } } @@ -192,7 +189,14 @@ void ThreadsManager::exit() { { threads[i].do_terminate = true; threads[i].wake_up(); - while (threads[i].state != Thread::TERMINATED) {} + +#if defined(_MSC_VER) + WaitForSingleObject(threads[i].handle, 0); + CloseHandle(threads[i].handle); +#else + pthread_join(threads[i].handle, NULL); + pthread_detach(threads[i].handle); +#endif } // Now we can safely destroy locks and wait conditions