#include <thread_pool.h>
Definition at line 25 of file thread_pool.h.
◆ Thread_Pool() [1/4]
Botan::Thread_Pool::Thread_Pool |
( |
std::optional< size_t > | pool_size | ) |
|
Initialize a thread pool with some number of threads
- Parameters
-
pool_size | number of threads in the pool, if 0 then some default value is chosen. If the optional is nullopt then the thread pool is disabled; all work is executed immediately when queued. |
Definition at line 50 of file thread_pool.cpp.
50 {
51 m_shutdown = false;
52
53 const std::string tname = "Botan thread";
54
55 if(!opt_pool_size.has_value()) {
56 return;
57 }
58
59 size_t pool_size = opt_pool_size.value();
60
61 if(pool_size == 0) {
63
64
65 if(pool_size == 0) {
66 pool_size = 2;
67 }
68
69
70
71
72
73 if(pool_size > 16) {
74 pool_size = 16;
75 }
76 }
77
78 m_workers.resize(pool_size);
79
80 for(size_t i = 0; i != pool_size; ++i) {
81 m_workers[i] = std::thread(&Thread_Pool::worker_thread, this);
82 OS::set_thread_name(m_workers[i], tname);
83 }
84}
size_t BOTAN_TEST_API get_cpu_available()
References Botan::OS::get_cpu_available().
◆ Thread_Pool() [2/4]
Botan::Thread_Pool::Thread_Pool |
( |
size_t | pool_size = 0 | ) |
|
|
inline |
Initialize a thread pool with some number of threads
- Parameters
-
pool_size | number of threads in the pool, if 0 then some default value is chosen. |
Definition at line 46 of file thread_pool.h.
Thread_Pool(std::optional< size_t > pool_size)
◆ ~Thread_Pool()
Botan::Thread_Pool::~Thread_Pool |
( |
| ) |
|
|
inline |
◆ Thread_Pool() [3/4]
Botan::Thread_Pool::Thread_Pool |
( |
const Thread_Pool & | | ) |
|
|
delete |
◆ Thread_Pool() [4/4]
◆ global_instance()
Return an instance to a shared thread pool
Definition at line 45 of file thread_pool.cpp.
45 {
46 static Thread_Pool g_thread_pool(global_thread_pool_size());
47 return g_thread_pool;
48}
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ queue_thunk()
void Botan::Thread_Pool::queue_thunk |
( |
const std::function< void()> & | fn | ) |
|
Definition at line 105 of file thread_pool.cpp.
105 {
106 std::unique_lock<std::mutex>
lock(m_mutex);
107
108 if(m_shutdown) {
109 throw Invalid_State("Cannot add work after thread pool has shut down");
110 }
111
112 if(m_workers.empty()) {
113 return fn();
114 }
115
116 m_tasks.push_back(fn);
117 m_more_tasks.notify_one();
118}
secure_vector< T > lock(const std::vector< T > &in)
References Botan::lock().
◆ run()
template<class F , class... Args>
auto Botan::Thread_Pool::run |
( |
F && | f, |
|
|
Args &&... | args ) -> std::future<typename std::invoke_result<F, Args...>::type> |
|
inline |
Definition at line 66 of file thread_pool.h.
66 {
67 using return_type = typename std::invoke_result<F, Args...>::type;
68
69 auto future_work = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
70 auto task = std::make_shared<std::packaged_task<return_type()>>(future_work);
71 auto future_result = task->get_future();
73 return future_result;
74 }
void queue_thunk(const std::function< void()> &)
◆ shutdown()
void Botan::Thread_Pool::shutdown |
( |
| ) |
|
Definition at line 86 of file thread_pool.cpp.
86 {
87 {
88 std::unique_lock<std::mutex>
lock(m_mutex);
89
90 if(m_shutdown == true) {
91 return;
92 }
93
94 m_shutdown = true;
95
96 m_more_tasks.notify_all();
97 }
98
99 for(auto&& thread : m_workers) {
100 thread.join();
101 }
102 m_workers.clear();
103}
References Botan::lock().
◆ worker_count()
size_t Botan::Thread_Pool::worker_count |
( |
| ) |
const |
|
inline |
Definition at line 52 of file thread_pool.h.
52{ return m_workers.size(); }
The documentation for this class was generated from the following files: