0.5.4
C++ to UML diagram generator based on Clang
Loading...
Searching...
No Matches
thread_pool_executor.h
Go to the documentation of this file.
1/**
2 * @file src/util/thread_pool_executor.h
3 *
4 * Copyright (c) 2021-2024 Bartek Kryza <bkryza@gmail.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#pragma once
19
20#include <deque>
21#include <future>
22#include <thread>
23#include <vector>
24
25namespace clanguml::util {
26
27/**
28 * @brief Simple thread pool executor for parallelizing diagram generation.
29 */
31public:
32 /**
33 * @brief Constructor
34 *
35 * @param pool_size Number of threads in the pool
36 */
37 explicit thread_pool_executor(unsigned int pool_size);
38
43
45
46 /**
47 * @brief Add a task to run on the pool.
48 *
49 * @param task Function to execute
50 * @return Future, allowing awaiting the result
51 */
52 std::future<void> add(std::function<void()> &&task);
53
54 /**
55 * @brief Join all active threads in the pool
56 */
57 void stop();
58
59private:
60 /**
61 * @brief Main worker pool thread method - take task from queue and execute
62 */
63 void worker();
64
65 std::packaged_task<void()> get();
66
67 std::atomic_bool done_;
68 std::deque<std::packaged_task<void()>> tasks_;
69 std::mutex tasks_mutex_;
70 std::condition_variable tasks_cond_;
71 std::vector<std::thread> threads_;
72};
73} // namespace clanguml::util