Threadpool
Loading...
Searching...
No Matches
threadpool.hpp
Go to the documentation of this file.
1/*
2** Copyright 2024 Maxtek Consulting
3**
4** Permission is hereby granted, free of charge, to any person obtaining a copy
5** of this software and associated documentation files (the "Software"), to deal
6** in the Software without restriction, including without limitation the rights
7** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8** copies of the Software, and to permit persons to whom the Software is
9** furnished to do so, subject to the following conditions:
10**
11** The above copyright notice and this permission notice shall be included in all
12** copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20** SOFTWARE.
21*/
22
23#ifndef THREADPOOL_HPP
24#define THREADPOOL_HPP
25
32#include <algorithm>
33#include <functional>
34#include <future>
35#include <memory>
36#include <queue>
37#include <thread>
38#include <typeinfo>
39#include <vector>
40
41#ifdef _WIN32
42#define DLL_EXPORT __declspec(dllexport)
43#else
44#define DLL_EXPORT
45#endif
46
47namespace maxtek
48{
55 {
56 public:
57
64 threadpool(size_t threads = std::thread::hardware_concurrency());
65
70
80 template <class F, class... Args>
81 std::future<std::result_of_t<F(Args...)>> submit(F &&function, Args &&...args)
82 {
83 std::shared_ptr<std::packaged_task<std::result_of_t<F(Args...)>()>> packaged_task;
84 std::function<void()> work;
85 std::future<std::result_of_t<F(Args...)>> result;
86
87 packaged_task = std::make_shared<std::packaged_task<std::result_of_t<F(Args...)>()>>(std::bind(std::forward<F>(function), std::forward<Args>(args)...));
88 result = packaged_task->get_future();
89
90 work = [packaged_task]()
91 {
92 (*packaged_task)();
93 };
94
95 push_task(std::move(work));
96
97 return result;
98 }
99
104 bool active() const;
105
110 void shutdown();
111
112 private:
113 void push_task(std::function<void()> &&task);
114 bool pop_task(std::function<void()> &task);
115
116 size_t num_threads;
117 bool _active;
118 std::vector<std::thread> _workers;
119 std::queue<std::function<void()>> _tasks;
120 std::mutex _mutex;
121 std::condition_variable _condition;
122 };
123}
124#endif
Definition threadpool.hpp:55
~threadpool()
destroys threadpool after calling shutdown if necessary
threadpool(size_t threads=std::thread::hardware_concurrency())
constructs a new threadpool
bool active() const
check if the threadpool is active
void shutdown()
shut down threadpool by joining threads and rejecting submissions
std::future< std::result_of_t< F(Args...)> > submit(F &&function, Args &&...args)
submits a function with its arguments to the threadpool
Definition threadpool.hpp:81
Definition threadpool.hpp:48
#define DLL_EXPORT
Definition threadpool.hpp:44