Botan 3.9.0
Crypto and TLS for C&
Botan::Thread_Pool Class Referencefinal

#include <thread_pool.h>

Public Member Functions

Thread_Pooloperator= (const Thread_Pool &)=delete
Thread_Pooloperator= (Thread_Pool &&)=delete
void queue_thunk (const std::function< void()> &work)
template<class F, class... Args>
auto run (F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
void shutdown ()
 Thread_Pool (const Thread_Pool &)=delete
 Thread_Pool (size_t pool_size=0)
 Thread_Pool (std::optional< size_t > pool_size)
 Thread_Pool (Thread_Pool &&)=delete
size_t worker_count () const
 ~Thread_Pool ()

Static Public Member Functions

static Thread_Poolglobal_instance ()

Detailed Description

Definition at line 25 of file thread_pool.h.

Constructor & Destructor Documentation

◆ Thread_Pool() [1/4]

Botan::Thread_Pool::Thread_Pool ( std::optional< size_t > pool_size)
explicit

Initialize a thread pool with some number of threads

Parameters
pool_sizenumber 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 61 of file thread_pool.cpp.

61 : m_shutdown(false) {
62 // On Linux, it is 16 length max, including terminator
63 const std::string tname = "Botan thread";
64
65 if(!opt_pool_size.has_value()) {
66 return;
67 }
68
69 size_t pool_size = opt_pool_size.value();
70
71 if(pool_size == 0) {
72 /*
73 * For large machines don't create too many threads, unless
74 * explicitly asked to by the caller.
75 */
76 const size_t cores = OS::get_cpu_available();
77 pool_size = std::clamp<size_t>(cores, 2, 16);
78 }
79
80 m_workers.resize(pool_size);
81
82 for(size_t i = 0; i != pool_size; ++i) {
83 m_workers[i] = std::thread(&Thread_Pool::worker_thread, this);
84 OS::set_thread_name(m_workers[i], tname);
85 }
86}
size_t BOTAN_TEST_API get_cpu_available()
Definition os_utils.cpp:236

Referenced by global_instance(), operator=(), operator=(), Thread_Pool(), Thread_Pool(), and Thread_Pool().

◆ Thread_Pool() [2/4]

Botan::Thread_Pool::Thread_Pool ( size_t pool_size = 0)
inlineexplicit

Initialize a thread pool with some number of threads

Parameters
pool_sizenumber of threads in the pool, if 0 then some default value is chosen.

Definition at line 46 of file thread_pool.h.

46: Thread_Pool(std::optional<size_t>(pool_size)) {}
Thread_Pool(std::optional< size_t > pool_size)

References Thread_Pool().

◆ ~Thread_Pool()

Botan::Thread_Pool::~Thread_Pool ( )
inline

Definition at line 48 of file thread_pool.h.

48{ shutdown(); }

References shutdown().

◆ Thread_Pool() [3/4]

Botan::Thread_Pool::Thread_Pool ( const Thread_Pool & )
delete

References Thread_Pool().

◆ Thread_Pool() [4/4]

Botan::Thread_Pool::Thread_Pool ( Thread_Pool && )
delete

References Thread_Pool().

Member Function Documentation

◆ global_instance()

Thread_Pool & Botan::Thread_Pool::global_instance ( )
static

Return an instance to a shared thread pool

Definition at line 56 of file thread_pool.cpp.

56 {
57 static Thread_Pool g_thread_pool(global_thread_pool_size());
58 return g_thread_pool;
59}

References Thread_Pool().

◆ operator=() [1/2]

Thread_Pool & Botan::Thread_Pool::operator= ( const Thread_Pool & )
delete

References Thread_Pool().

◆ operator=() [2/2]

Thread_Pool & Botan::Thread_Pool::operator= ( Thread_Pool && )
delete

References queue_thunk(), and Thread_Pool().

◆ queue_thunk()

void Botan::Thread_Pool::queue_thunk ( const std::function< void()> & work)

Definition at line 107 of file thread_pool.cpp.

107 {
108 std::unique_lock<std::mutex> lock(m_mutex);
109
110 if(m_shutdown) {
111 throw Invalid_State("Cannot add work after thread pool has shut down");
112 }
113
114 if(m_workers.empty()) {
115 return work();
116 }
117
118 m_tasks.push_back(work);
119 m_more_tasks.notify_one();
120}
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:81

References Botan::lock().

Referenced by operator=(), and run().

◆ run()

template<class F, class... Args>
auto Botan::Thread_Pool::run ( F && f,
Args &&... args ) -> std::future<std::invoke_result_t<F, Args...>>
inline

Definition at line 66 of file thread_pool.h.

66 {
67 using return_type = std::invoke_result_t<F, Args...>;
68
69 auto future_work = std::bind(std::forward<F>(f), std::forward<Args>(args)...); // NOLINT(*-avoid-bind)
70 auto task = std::make_shared<std::packaged_task<return_type()>>(future_work);
71 auto future_result = task->get_future();
72 queue_thunk([task]() { (*task)(); });
73 return future_result;
74 }
void queue_thunk(const std::function< void()> &work)

References queue_thunk().

◆ shutdown()

void Botan::Thread_Pool::shutdown ( )

Definition at line 88 of file thread_pool.cpp.

88 {
89 {
90 std::unique_lock<std::mutex> lock(m_mutex);
91
92 if(m_shutdown) {
93 return;
94 }
95
96 m_shutdown = true;
97
98 m_more_tasks.notify_all();
99 }
100
101 for(auto&& thread : m_workers) {
102 thread.join();
103 }
104 m_workers.clear();
105}

References Botan::lock().

Referenced by ~Thread_Pool().

◆ 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: