errors
· 1.7 KiB · Text
Raw
/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
79 | [promise = std::move(promise), func = std::move(func)]() mutable {
std::function<void()> f1(std::move(task));
std::function<void()> f2 ([t = std::move(task)]() mutable { t(); });
/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
74 | std::function<void()> f1(std::move(task));
| ^
/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
75 | std::function<void()> f2 ([t = std::move(task)]() mutable { t(); });
queue.emplace([t = std::move(task)]() mutable { t(); });
/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
73 | queue.emplace([t = std::move(task)]() mutable { t(); });
| ^
/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
1604 | packaged_task(const packaged_task&) = delete;
| ^
| 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 | |
| 15 | queue.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
· 3.2 KiB · C++
Raw
/**
*******************************************
* @file libcpu/threadPool/include/thread_pool.hpp
* @version 1.0
* @date 8-December-2024
* @brief thread pool
* @note https://git.leshe4ka.ru/leshe4ka/oop24/src/branch/lab3/3
*******************************************
*/
#ifndef THREAD_POOL_HPP_INC_
#define THREAD_POOL_HPP_INC_
#include "vector/vector.hpp" // IWYU pragma: export
#include <any>
#include <atomic>
#include <condition_variable> // IWYU pragma: export
#include <cstdint> // IWYU pragma: export
#include <functional> // IWYU pragma: export
#include <future> // IWYU pragma: export
#include <list>
#include <memory> // IWYU pragma: export
#include <mutex> // IWYU pragma: export
#include <queue> // IWYU pragma: export
#include <thread> // IWYU pragma: export
#include <vector> // IWYU pragma: export
namespace cpu_n {
// template <typename F, typename... Args> class MyBind {
// public:
// explicit MyBind(F &&f, Args &&...args)
// : f_(std::forward<F>(f)),
// args_(std::make_tuple(std::forward<Args>(args)...)) {}
// auto operator()() { return std::apply(f_, args_); }
// private:
// std::function<std::invoke_result_t<F, Args...>()> f_;
// std::tuple<Args...> args_;
// };
class ThreadPool {
public:
// NOLINTNEXTLINE
ThreadPool(uint64_t threads = std::thread::hardware_concurrency());
~ThreadPool();
void start();
template <typename F, typename... Args>
auto enqueue(F &&f, Args &&...args) -> std::future<decltype(f(args...))> {
if (worker_count->load() != workers.size()) {
start();
}
auto func = [f = std::forward<F>(f),
... args = std::forward<Args>(args)]() mutable {
return f(args...);
};
auto encapsulated_ptr =
std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
// auto task = std::packaged_task<decltype(f(args...))()>(func);
std::future<decltype(f(args...))> future_object = encapsulated_ptr->get_future();
// std::future<decltype(f(args...))> future_object = task.get_future();
{
std::lock_guard<std::mutex> lock(*mutexPtr);
queue.emplace([encapsulated_ptr](){ (*encapsulated_ptr)();});
// queue.emplace([t = std::move(task)]() mutable { t(); });
}
cvPtr->notify_one();
return future_object;
}
ThreadPool(const ThreadPool &) = delete;
ThreadPool(ThreadPool &&o) noexcept = default;
ThreadPool &operator=(const ThreadPool &) = delete;
ThreadPool &operator=(ThreadPool &&o) = default;
[[nodiscard]] auto threadsNum() const { return threads; }
[[nodiscard]] auto started() const { return started_state; }
private:
vec::vector<std::thread> workers;
std::vector<bool> worker_up;
std::unique_ptr<std::atomic<uint64_t>> worker_count;
std::unique_ptr<std::mutex> mutexPtr;
std::unique_ptr<std::condition_variable> cvPtr;
std::queue<std::function<void()>,std::list<std::function<void()>>> queue;
void worker(uint64_t thread_id);
bool stop;
uint64_t threads;
bool started_state = false;
};
} // namespace cpu_n
#endif
| 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 | |
| 29 | namespace 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 | |
| 44 | class 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 |