// // Created by 马也驰 on 2024/12/28. // #ifndef LEVELDB_THREADPOOL_H #define LEVELDB_THREADPOOL_H #include #include #include #include #include #include #include class ThreadPool { public: explicit ThreadPool(size_t numThreads) : stop(false) { for (size_t i = 0; i < numThreads; ++i) { workers.emplace_back([this]() { while (true) { std::function task; { std::unique_lock lock(queueMutex); condition.wait(lock, [this]() { return stop || !tasks.empty(); }); if (stop && tasks.empty()) return; task = std::move(tasks.front()); tasks.pop(); } task(); } }); } } ~ThreadPool() { { std::unique_lock lock(queueMutex); stop = true; } condition.notify_all(); for (std::thread &worker : workers) { worker.join(); } } void enqueue(std::function task) { { std::unique_lock lock(queueMutex); tasks.push(std::move(task)); } condition.notify_one(); } private: std::vector workers; std::queue> tasks; std::mutex queueMutex; std::condition_variable condition; std::atomic stop; }; #endif // LEVELDB_THREADPOOL_H