// // ThreadPool.hpp // NitroModules // // Created by Marc Rousavy on 21.06.24. // #include #include #include #include #include #include #include #include #include namespace margelo::nitro { class ThreadPool final { public: /** * Create a new ThreadPool with the given number of fixed workers/threads. */ explicit ThreadPool(const char* const name, size_t numThreads); ~ThreadPool(); /** * Schedules the given task asynchronously on the ThreadPool. * It will run once a worker is available. */ void run(std::function&& task); public: /** * Get a static singleton instance - a shared ThreadPool. */ static std::shared_ptr getSharedPool(); private: std::vector _workers; std::queue> _tasks; std::mutex _queueMutex; std::condition_variable _condition; std::atomic _isAlive; const char* _name; static constexpr auto TAG = "ThreadPool"; }; } // namespace margelo::nitro