Kaspersky Neuromorphic Platform  1.0.0
API Reference
Loading...
Searching...
No Matches
thread_pool_context.h
Go to the documentation of this file.
1
21#pragma once
22
23#include <condition_variable>
24#include <memory>
25#include <mutex>
26#include <queue>
27#include <thread>
28#include <utility>
29
30#include <boost/asio/thread_pool.hpp>
31#include <boost/asio/ts/executor.hpp>
32
33
38{
39
46{
47public:
52 explicit ThreadPoolContext(size_t num_threads = std::thread::hardware_concurrency());
53
59
60 // Move and assignment are implicitly deleted because of mutex.
61
62
63private:
64 enum class Usage
65 {
66 FINISHED,
67 READY,
68 WORKING,
69 STOPPING
70 };
71
72 struct Function
73 {
74 std::shared_ptr<size_t> work_count_;
75 // cppcheck-suppress unusedStructMember
76 void (*execute_)(std::shared_ptr<Function> &p);
77 };
78
79 void do_work_started(const std::shared_ptr<size_t> &task_count) noexcept;
80
81 void do_work_finished(const std::shared_ptr<size_t> &task_count);
82
83 void execute(std::unique_lock<std::mutex> &lock, std::shared_ptr<Function> &work);
84
85 bool execute_next(std::unique_lock<std::mutex> &lock);
86
87 void stop();
88
89 void post(const std::shared_ptr<Function> &task, const std::shared_ptr<size_t> &task_count);
90
91private:
95 friend class ThreadPoolExecutor;
96 std::mutex mutex_;
97 std::condition_variable condition_;
98 Usage usage_state_ = Usage::READY;
99 // cppcheck-suppress unusedStructMember
100 std::queue<std::shared_ptr<Function>> work_queue_;
101 boost::asio::thread_pool pool_;
102};
103
104} // namespace knp::backends::cpu_executors
The ThreadPoolContext class is a service class used for creating pool executors.
ThreadPoolContext(size_t num_threads=std::thread::hardware_concurrency())
Constructor.
friend class ThreadPoolExecutor
The ThreadPoolExecutor class is a definition of the interface to thread pool used for thread executio...
Namespace for CPU backend executors.
Definition backend.h:50