/*1Stockfish, a UCI chess playing engine derived from Glaurung 2.12Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)34Stockfish is free software: you can redistribute it and/or modify5it under the terms of the GNU General Public License as published by6the Free Software Foundation, either version 3 of the License, or7(at your option) any later version.89Stockfish is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12GNU General Public License for more details.1314You should have received a copy of the GNU General Public License15along with this program. If not, see <http://www.gnu.org/licenses/>.16*/1718#ifndef THREAD_WIN32_OSX_H_INCLUDED19#define THREAD_WIN32_OSX_H_INCLUDED2021#include <thread>2223// On OSX threads other than the main thread are created with a reduced stack24// size of 512KB by default, this is too low for deep searches, which require25// somewhat more than 1MB stack, so adjust it to TH_STACK_SIZE.26// The implementation calls pthread_create() with the stack size parameter27// equal to the Linux 8MB default, on platforms that support it.2829#if defined(__APPLE__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(USE_PTHREADS)3031#include <pthread.h>32#include <functional>3334namespace Stockfish {3536class NativeThread {37pthread_t thread;3839static constexpr size_t TH_STACK_SIZE = 8 * 1024 * 1024;4041public:42template<class Function, class... Args>43explicit NativeThread(Function&& fun, Args&&... args) {44auto func = new std::function<void()>(45std::bind(std::forward<Function>(fun), std::forward<Args>(args)...));4647pthread_attr_t attr_storage, *attr = &attr_storage;48pthread_attr_init(attr);49pthread_attr_setstacksize(attr, TH_STACK_SIZE);5051auto start_routine = [](void* ptr) -> void* {52auto f = reinterpret_cast<std::function<void()>*>(ptr);53// Call the function54(*f)();55delete f;56return nullptr;57};5859pthread_create(&thread, attr, start_routine, func);60}6162void join() { pthread_join(thread, nullptr); }63};6465} // namespace Stockfish6667#else // Default case: use STL classes6869namespace Stockfish {7071using NativeThread = std::thread;7273} // namespace Stockfish7475#endif7677#endif // #ifndef THREAD_WIN32_OSX_H_INCLUDED787980