AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
threadPool.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <chrono>
23#include <condition_variable>
24#include <cstdint>
25#include <functional>
26#include <mutex>
27#include <queue>
28#include <thread>
29#include <vector>
30
31namespace anch {
32
50 class ThreadPool {
51
52 // Attributes +
53 private:
55 std::mutex _mutex;
56
58 std::condition_variable _taskCV;
59
61 std::condition_variable _idleCV;
62
64 std::queue<std::move_only_function<void()>> _tasks;
65
67 uint32_t _maxThreads;
68
70 uint32_t _active;
71
73 bool _running;
74
76 bool _terminating;
77
79 bool _shutdown;
80
82 std::vector<std::jthread> _workers;
83 // Attributes -
84
85 // Constructors +
86 public:
92 ThreadPool(uint32_t maxThreads = 0);
93
97 ThreadPool(const ThreadPool&) = delete;
98
103 // Constructors -
104
105 // Destructor +
106 public:
112 // Destructor -
113
114 // Operators +
115 public:
119 ThreadPool& operator=(const ThreadPool&) = delete;
120
125 // Operators -
126
127 // Methods +
128 public:
132 void start();
133
138 void stop();
139
149 template<typename R, typename P = std::ratio<1>>
150 void awaitTermination(std::chrono::duration<R,P> duration);
151
161 template<typename Func, typename... Args>
162 bool add(Func function, Args... args);
163
164 private:
168 void worker();
169 // Methods -
170
171 };
172
173}
174
175#include "impl/threadPool.hpp"
void awaitTermination(std::chrono::duration< R, P > duration)
ThreadPool(ThreadPool &&)=delete
ThreadPool & operator=(const ThreadPool &)=delete
ThreadPool(uint32_t maxThreads=0)
bool add(Func function, Args... args)
ThreadPool(const ThreadPool &)=delete
ThreadPool & operator=(ThreadPool &&)=delete
AnCH framework base namespace.
Definition app.hpp:30