Botan 2.19.1
Crypto and TLS for C&
Public Member Functions | Static Public Member Functions | List of all members
Botan::Thread_Pool Class Reference

#include <thread_pool.h>

Public Member Functions

Thread_Pooloperator= (const Thread_Pool &)=delete
 
Thread_Pooloperator= (Thread_Pool &&)=delete
 
void queue_thunk (std::function< void()>)
 
template<class F , class... Args>
auto run (F &&f, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
 
void shutdown ()
 
 Thread_Pool (const Thread_Pool &)=delete
 
 Thread_Pool (size_t pool_size=0)
 
 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 24 of file thread_pool.h.

Constructor & Destructor Documentation

◆ Thread_Pool() [1/3]

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

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

22 {
23 if(pool_size == 0)
24 {
25 pool_size = OS::get_cpu_available();
26
27 /*
28 * For large machines don't create too many threads, unless
29 * explicitly asked to by the caller.
30 */
31 if(pool_size > 16)
32 pool_size = 16;
33 }
34
35 if(pool_size <= 1)
36 pool_size = 2;
37
38 m_shutdown = false;
39
40 for(size_t i = 0; i != pool_size; ++i)
41 {
42 m_workers.push_back(std::thread(&Thread_Pool::worker_thread, this));
43 }
44 }
size_t BOTAN_TEST_API get_cpu_available()
Definition: os_utils.cpp:230

References Botan::OS::get_cpu_available().

◆ ~Thread_Pool()

Botan::Thread_Pool::~Thread_Pool ( )
inline

Definition at line 39 of file thread_pool.h.

39{ shutdown(); }

◆ Thread_Pool() [2/3]

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

◆ Thread_Pool() [3/3]

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

16 {
17 static Thread_Pool g_thread_pool(OS::read_env_variable_sz("BOTAN_THREAD_POOL_SIZE"));
18 return g_thread_pool;
19 }
Thread_Pool(size_t pool_size=0)
Definition: thread_pool.cpp:21
size_t read_env_variable_sz(const std::string &var_name, size_t def_value=0)
Definition: os_utils.cpp:413

References Botan::OS::read_env_variable_sz().

Referenced by Botan::XMSS_PrivateKey::tree_hash().

◆ 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 ( std::function< void()>  fn)

Definition at line 66 of file thread_pool.cpp.

67 {
68 std::unique_lock<std::mutex> lock(m_mutex);
69
70 if(m_shutdown)
71 throw Invalid_State("Cannot add work after thread pool has shut down");
72
73 m_tasks.push_back(fn);
74 m_more_tasks.notify_one();
75 }

◆ run()

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

Definition at line 57 of file thread_pool.h.

58 {
59 typedef typename std::result_of<F(Args...)>::type return_type;
60
61 auto future_work = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
62 auto task = std::make_shared<std::packaged_task<return_type ()>>(future_work);
63 auto future_result = task->get_future();
64 queue_thunk([task]() { (*task)(); });
65 return future_result;
66 }
void queue_thunk(std::function< void()>)
Definition: thread_pool.cpp:66
MechanismType type

References type.

Referenced by Botan::XMSS_PrivateKey::tree_hash().

◆ shutdown()

void Botan::Thread_Pool::shutdown ( )

Definition at line 46 of file thread_pool.cpp.

47 {
48 {
49 std::unique_lock<std::mutex> lock(m_mutex);
50
51 if(m_shutdown == true)
52 return;
53
54 m_shutdown = true;
55
56 m_more_tasks.notify_all();
57 }
58
59 for(auto&& thread : m_workers)
60 {
61 thread.join();
62 }
63 m_workers.clear();
64 }

◆ worker_count()

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

Definition at line 43 of file thread_pool.h.

43{ return m_workers.size(); }

Referenced by Botan::XMSS_PrivateKey::tree_hash().


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