Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
ThreadPool.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** R-Type
4** File description:
5** ThreadPool.hpp, ThreadPool class declaration
6*/
7
8/*
9** MIT License
10**
11** Copyright (c) 2025 Robin Toillon
12**
13** Permission is hereby granted, free of charge, to any person obtaining
14** a copy of this software and associated documentation files (the
15** "Software"), to deal in the Software without restriction, including
16** without limitation the rights to use, copy, modify, merge, publish,
17** distribute, sublicense, and/or sell copies of the Software, and to
18** permit persons to whom the Software is furnished to do so, subject to
19** the following conditions:
20**
21** The above copyright notice and this permission notice shall be
22** included in all copies or substantial portions of the Software.
23**
24** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31*/
32
44#ifndef RTYPE_THREADPOOL_HPP_
45 #define RTYPE_THREADPOOL_HPP_
46
47 #include "RType/Error.hpp"
48
49 #include <concepts>
50 #include <condition_variable>
51 #include <expected>
52 #include <functional>
53 #include <memory>
54 #include <future>
55 #include <mutex>
56 #include <queue>
57 #include <thread>
58 #include <vector>
59 #include <functional>
60
61namespace rtp::thread
62{
74 class ThreadPool final {
75 public:
76 ThreadPool(const ThreadPool &) = delete;
77 ThreadPool &operator=(const ThreadPool &) = delete;
78
92 [[nodiscard]]
93 static auto create(size_t numThreads)
94 -> std::expected<std::unique_ptr<ThreadPool>, rtp::Error>;
95
103 ~ThreadPool() noexcept;
104
121 template<std::invocable F, typename... Args>
122 [[nodiscard]]
123 auto enqueue(F &&f, Args &&...args)
124 -> std::expected<std::future<std::invoke_result_t<F, Args...>>,
125 rtp::Error>;
126
127 private:
128 std::vector<std::jthread> _workers;
130 std::queue<std::move_only_function<void(void)>> _tasks;
135 std::condition_variable _condition;
138 bool _stop{false};
141 private:
150 ThreadPool(void) noexcept = default;
151
162 void start(size_t numThreads);
163
175 void workerThread(std::stop_token stopToken) noexcept;
176
177 };
178}
179
180 #include "ThreadPool.tpp" /* Enqueue method implementation */
181
182#endif /* !RTYPE_THREADPOOL_HPP_ */
Error handling system with categorized error codes.
Comprehensive error object with severity and retry tracking.
Definition Error.hpp:156
ThreadPool class for managing a pool of threads.
ThreadPool(void) noexcept=default
Private constructor to initialize the ThreadPool.
bool _stop
Flag indicating whether the ThreadPool is stopping.
void workerThread(std::stop_token stopToken) noexcept
The worker thread function that continuously processes tasks.
std::mutex _queueMutex
Mutex for synchronizing access to the task queue.
static auto create(size_t numThreads) -> std::expected< std::unique_ptr< ThreadPool >, rtp::Error >
Creates a ThreadPool with the specified number of threads.
ThreadPool(const ThreadPool &)=delete
ThreadPool & operator=(const ThreadPool &)=delete
std::vector< std::jthread > _workers
Vector of worker threads.
void start(size_t numThreads)
Starts the worker threads in the pool.
~ThreadPool() noexcept
Destroys the ThreadPool, ensuring all threads are joined and resources are released.
std::condition_variable _condition
Condition variable for notifying worker threads.
auto enqueue(F &&f, Args &&...args) -> std::expected< std::future< std::invoke_result_t< F, Args... > >, rtp::Error >
Enqueues a task for asynchronous execution.
std::queue< std::move_only_function< void(void)> > _tasks
Queue of tasks to be executed.
STL namespace.