Last active 1735297410

Revision ad627ba1e73a145bef9b6c9634ac5bd716bceb2c

errors Raw
1/home/alex/oop24/3/libcpu/thread_pool/include/thread_pool.hpp:79:18: note: copy constructor of '(lambda at /home/alex/oop24/3/libcpu/thread_pool/include/thread_pool.hpp:79:17)' is implicitly deleted because field '' has a deleted copy constructor
2 79 | [promise = std::move(promise), func = std::move(func)]() mutable {
3
4
5 std::function<void()> f1(std::move(task));
6 std::function<void()> f2 ([t = std::move(task)]() mutable { t(); });
7
8/home/alex/oop24/3/libcpu/thread_pool/include/thread_pool.hpp:74:35: note: in instantiation of function template specialization 'std::function<void ()>::function<std::packaged_task<void ()>, void>' requested here
9 74 | std::function<void()> f1(std::move(task));
10 | ^
11/home/alex/oop24/3/libcpu/thread_pool/include/thread_pool.hpp:75:35: note: in instantiation of function template specialization 'std::function<void ()>::function<(lambda at /home/alex/oop24/3/libcpu/thread_pool/include/thread_pool.hpp:75:39), void>' requested here
12 75 | std::function<void()> f2 ([t = std::move(task)]() mutable { t(); });
13
14
15queue.emplace([t = std::move(task)]() mutable { t(); });
16/home/alex/oop24/3/libcpu/thread_pool/include/thread_pool.hpp:73:28: note: copy constructor of '(lambda at /home/alex/oop24/3/libcpu/thread_pool/include/thread_pool.hpp:73:27)' is implicitly deleted because field '' has a deleted copy constructor
17 73 | queue.emplace([t = std::move(task)]() mutable { t(); });
18 | ^
19/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/future:1604:7: note: 'packaged_task' has been explicitly marked deleted here
20 1604 | packaged_task(const packaged_task&) = delete;
21 | ^
threadPool.hpp Raw
1/**
2 *******************************************
3 * @file libcpu/threadPool/include/thread_pool.hpp
4 * @version 1.0
5 * @date 8-December-2024
6 * @brief thread pool
7 * @note https://git.leshe4ka.ru/leshe4ka/oop24/src/branch/lab3/3
8 *******************************************
9 */
10
11#ifndef THREAD_POOL_HPP_INC_
12#define THREAD_POOL_HPP_INC_
13
14#include "vector/vector.hpp" // IWYU pragma: export
15
16#include <any>
17#include <atomic>
18#include <condition_variable> // IWYU pragma: export
19#include <cstdint> // IWYU pragma: export
20#include <functional> // IWYU pragma: export
21#include <future> // IWYU pragma: export
22#include <list>
23#include <memory> // IWYU pragma: export
24#include <mutex> // IWYU pragma: export
25#include <queue> // IWYU pragma: export
26#include <thread> // IWYU pragma: export
27#include <vector> // IWYU pragma: export
28
29namespace cpu_n {
30// template <typename F, typename... Args> class MyBind {
31// public:
32// explicit MyBind(F &&f, Args &&...args)
33// : f_(std::forward<F>(f)),
34// args_(std::make_tuple(std::forward<Args>(args)...)) {}
35
36// auto operator()() { return std::apply(f_, args_); }
37
38// private:
39
40// std::function<std::invoke_result_t<F, Args...>()> f_;
41// std::tuple<Args...> args_;
42// };
43
44class ThreadPool {
45 public:
46 // NOLINTNEXTLINE
47 ThreadPool(uint64_t threads = std::thread::hardware_concurrency());
48 ~ThreadPool();
49
50 void start();
51
52 template <typename F, typename... Args>
53 auto enqueue(F &&f, Args &&...args) -> std::future<decltype(f(args...))> {
54 if (worker_count->load() != workers.size()) {
55 start();
56 }
57
58 auto func = [f = std::forward<F>(f),
59 ... args = std::forward<Args>(args)]() mutable {
60 return f(args...);
61 };
62
63 auto encapsulated_ptr =
64 std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
65 // auto task = std::packaged_task<decltype(f(args...))()>(func);
66
67 std::future<decltype(f(args...))> future_object = encapsulated_ptr->get_future();
68 // std::future<decltype(f(args...))> future_object = task.get_future();
69
70 {
71 std::lock_guard<std::mutex> lock(*mutexPtr);
72 queue.emplace([encapsulated_ptr](){ (*encapsulated_ptr)();});
73 // queue.emplace([t = std::move(task)]() mutable { t(); });
74 }
75 cvPtr->notify_one();
76 return future_object;
77 }
78
79 ThreadPool(const ThreadPool &) = delete;
80 ThreadPool(ThreadPool &&o) noexcept = default;
81 ThreadPool &operator=(const ThreadPool &) = delete;
82 ThreadPool &operator=(ThreadPool &&o) = default;
83
84 [[nodiscard]] auto threadsNum() const { return threads; }
85 [[nodiscard]] auto started() const { return started_state; }
86
87 private:
88 vec::vector<std::thread> workers;
89 std::vector<bool> worker_up;
90 std::unique_ptr<std::atomic<uint64_t>> worker_count;
91 std::unique_ptr<std::mutex> mutexPtr;
92 std::unique_ptr<std::condition_variable> cvPtr;
93 std::queue<std::function<void()>,std::list<std::function<void()>>> queue;
94 void worker(uint64_t thread_id);
95 bool stop;
96
97 uint64_t threads;
98 bool started_state = false;
99};
100
101} // namespace cpu_n
102#endif
103