Botan 3.0.0
Crypto and TLS for C&
Public Member Functions | Static Public Member Functions | List of all members
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()> &)
 
template<class F , class... Args>
auto run (F &&f, Args &&... args) -> std::future< typename std::invoke_result< F, Args... >::type >
 
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)

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 44 of file thread_pool.cpp.

45 {
46 m_shutdown = false;
47
48 if(!opt_pool_size.has_value())
49 return;
50
51 size_t pool_size = opt_pool_size.value();
52
53 if(pool_size == 0)
54 {
55 pool_size = OS::get_cpu_available();
56
57 // Unclear if this can happen, but be defensive
58 if(pool_size == 0)
59 pool_size = 2;
60
61 /*
62 * For large machines don't create too many threads, unless
63 * explicitly asked to by the caller.
64 */
65 if(pool_size > 16)
66 pool_size = 16;
67 }
68
69 for(size_t i = 0; i != pool_size; ++i)
70 {
71 m_workers.push_back(std::thread(&Thread_Pool::worker_thread, this));
72 }
73 }
size_t BOTAN_TEST_API get_cpu_available()
Definition: os_utils.cpp:238

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_sizenumber of threads in the pool, if 0 then some default value is chosen.

Definition at line 47 of file thread_pool.h.

47 :
48 Thread_Pool(std::optional<size_t>(pool_size))
49 {}
Thread_Pool(std::optional< size_t > pool_size)
Definition: thread_pool.cpp:44

◆ ~Thread_Pool()

Botan::Thread_Pool::~Thread_Pool ( )
inline

Definition at line 51 of file thread_pool.h.

51{ shutdown(); }

◆ Thread_Pool() [3/4]

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

◆ Thread_Pool() [4/4]

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

Member Function Documentation

◆ global_instance()

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

Return an instance to a shared thread pool

Definition at line 38 of file thread_pool.cpp.

39 {
40 static Thread_Pool g_thread_pool(global_thread_pool_size());
41 return g_thread_pool;
42 }

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ queue_thunk()

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

Definition at line 95 of file thread_pool.cpp.

96 {
97 std::unique_lock<std::mutex> lock(m_mutex);
98
99 if(m_shutdown)
100 throw Invalid_State("Cannot add work after thread pool has shut down");
101
102 if(m_workers.empty())
103 {
104 return fn();
105 }
106
107 m_tasks.push_back(fn);
108 m_more_tasks.notify_one();
109 }
secure_vector< T > lock(const std::vector< T > &in)
Definition: secmem.h:71

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 69 of file thread_pool.h.

70 {
71 using return_type = typename std::invoke_result<F, Args...>::type;
72
73 auto future_work = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
74 auto task = std::make_shared<std::packaged_task<return_type ()>>(future_work);
75 auto future_result = task->get_future();
76 queue_thunk([task]() { (*task)(); });
77 return future_result;
78 }
void queue_thunk(const std::function< void()> &)
Definition: thread_pool.cpp:95

◆ shutdown()

void Botan::Thread_Pool::shutdown ( )

Definition at line 75 of file thread_pool.cpp.

76 {
77 {
78 std::unique_lock<std::mutex> lock(m_mutex);
79
80 if(m_shutdown == true)
81 return;
82
83 m_shutdown = true;
84
85 m_more_tasks.notify_all();
86 }
87
88 for(auto&& thread : m_workers)
89 {
90 thread.join();
91 }
92 m_workers.clear();
93 }

References Botan::lock().

◆ worker_count()

size_t Botan::Thread_Pool::worker_count ( ) const
inline

Definition at line 55 of file thread_pool.h.

55{ return m_workers.size(); }

The documentation for this class was generated from the following files: