diff --git a/ThreadPool.h b/ThreadPool.h index 418320307885dc26efb07ee39cc159aa72b13f0d..56627ea722465e45877a79e38e74a2211250e979 100644 --- a/ThreadPool.h +++ b/ThreadPool.h @@ -19,18 +19,18 @@ public: -> std::future::type>; ~ThreadPool(); private: - // need to keep track of threads so we can join them + // need to keep track of threads, so we can join them / 需要跟踪线程,这样我们才能 join 他们 std::vector< std::thread > workers; - // the task queue + // the task queue / 任务队列 std::queue< std::function > tasks; - // synchronization + // synchronization / 同步 std::mutex queue_mutex; std::condition_variable condition; bool stop; }; -// the constructor just launches some amount of workers +// the constructor just launches some amount of workers / 构造函数只是启动一定数量的work inline ThreadPool::ThreadPool(size_t threads) : stop(false) { @@ -58,7 +58,7 @@ inline ThreadPool::ThreadPool(size_t threads) ); } -// add new work item to the pool +// add new work item to the pool / 向池中添加新的work template auto ThreadPool::enqueue(F&& f, Args&&... args) -> std::future::type> @@ -73,7 +73,7 @@ auto ThreadPool::enqueue(F&& f, Args&&... args) { std::unique_lock lock(queue_mutex); - // don't allow enqueueing after stopping the pool + // don't allow enqueueing after stopping the pool / 停止池后不允许排队 if(stop) throw std::runtime_error("enqueue on stopped ThreadPool"); @@ -83,7 +83,7 @@ auto ThreadPool::enqueue(F&& f, Args&&... args) return res; } -// the destructor joins all threads +// the destructor joins all threads / 析构函数连接所有线程 inline ThreadPool::~ThreadPool() { {