diff --git a/ThreadPool.h b/ThreadPool.h index 4183203..3f20b08 100644 --- a/ThreadPool.h +++ b/ThreadPool.h @@ -10,31 +10,37 @@ #include #include #include +#include class ThreadPool { public: ThreadPool(size_t); template auto enqueue(F&& f, Args&&... args) - -> std::future::type>; + -> std::future::type>; ~ThreadPool(); + + // 获取当前任务队列大小 + size_t getTaskCount(); + // 获取当前活跃线程数 + size_t getActiveThreadCount(); + private: - // need to keep track of threads so we can join them - std::vector< std::thread > workers; - // the task queue - std::queue< std::function > tasks; + // 跟踪线程以便我们可以加入它们 + std::vector workers; + // 任务队列 + std::queue> tasks; - // synchronization + // 同步 std::mutex queue_mutex; std::condition_variable condition; bool stop; }; - -// the constructor just launches some amount of workers + inline ThreadPool::ThreadPool(size_t threads) - : stop(false) + : stop(false) { - for(size_t i = 0;itasks.pop(); } - task(); + try { + task(); + } catch (const std::exception& e) { + // 处理任务中的异常 + } } } ); } -// add new work item to the pool template auto ThreadPool::enqueue(F&& f, Args&&... args) - -> std::future::type> + -> std::future::type> { - using return_type = typename std::result_of::type; + using return_type = typename std::invoke_result::type; - auto task = std::make_shared< std::packaged_task >( + auto task = std::make_shared>( std::bind(std::forward(f), std::forward(args)...) ); @@ -73,7 +82,6 @@ auto ThreadPool::enqueue(F&& f, Args&&... args) { std::unique_lock lock(queue_mutex); - // don't allow enqueueing after stopping the pool if(stop) throw std::runtime_error("enqueue on stopped ThreadPool"); @@ -83,7 +91,6 @@ auto ThreadPool::enqueue(F&& f, Args&&... args) return res; } -// the destructor joins all threads inline ThreadPool::~ThreadPool() { { @@ -95,4 +102,14 @@ inline ThreadPool::~ThreadPool() worker.join(); } +size_t ThreadPool::getTaskCount() { + std::unique_lock lock(queue_mutex); + return tasks.size(); +} + +size_t ThreadPool::getActiveThreadCount() { + std::unique_lock lock(queue_mutex); + return workers.size(); +} + #endif