| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * License); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| #ifndef COMMON_THREAD_POOL_H |
| #define COMMON_THREAD_POOL_H |
| |
| #include <condition_variable> |
| #include <functional> |
| #include <future> |
| #include <mutex> |
| #include <queue> |
| #include <thread> |
| #include <vector> |
| |
| namespace common { |
| |
| class ThreadPool { |
| public: |
| explicit ThreadPool(size_t num_threads) : stop_(false) { |
| for (size_t i = 0; i < num_threads; i++) { |
| workers_.emplace_back([this] { |
| for (;;) { |
| std::function<void()> task; |
| { |
| std::unique_lock<std::mutex> lock(mutex_); |
| cv_.wait(lock, |
| [this] { return stop_ || !tasks_.empty(); }); |
| if (stop_ && tasks_.empty()) return; |
| task = std::move(tasks_.front()); |
| tasks_.pop(); |
| } |
| task(); |
| } |
| }); |
| } |
| } |
| |
| ~ThreadPool() { |
| { |
| std::unique_lock<std::mutex> lock(mutex_); |
| stop_ = true; |
| } |
| cv_.notify_all(); |
| for (auto& w : workers_) w.join(); |
| } |
| |
| template <typename F> |
| std::future<typename std::result_of<F()>::type> submit(F&& f) { |
| using RetType = typename std::result_of<F()>::type; |
| auto task = std::make_shared<std::packaged_task<RetType()>>( |
| std::forward<F>(f)); |
| std::future<RetType> result = task->get_future(); |
| { |
| std::unique_lock<std::mutex> lock(mutex_); |
| tasks_.emplace([task]() { (*task)(); }); |
| } |
| cv_.notify_one(); |
| return result; |
| } |
| |
| private: |
| std::vector<std::thread> workers_; |
| std::queue<std::function<void()>> tasks_; |
| std::mutex mutex_; |
| std::condition_variable cv_; |
| bool stop_; |
| }; |
| |
| } // namespace common |
| |
| #endif // COMMON_THREAD_POOL_H |