1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/*!
@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause
*/
#pragma once
#include <deque>
#include <vector>
#include <pyclustering/parallel/thread_executor.hpp>
namespace pyclustering {
namespace parallel {
/*!
@class thread_pool thread_pool.hpp pyclustering/parallel/thread_pool.hpp
@brief Thread pool provides service to execute client tasks asynchronously in parallel way.
*/
class thread_pool {
public:
/*!
@brief Defines shared pointer of the thread pool.
*/
using ptr = std::shared_ptr<thread_pool>;
private:
using thread_container = std::vector<thread_executor::ptr>;
public:
static const std::size_t DEFAULT_AMOUNT_THREADS; /**< Default amount of threads. */
static const std::size_t DEFAULT_POOL_SIZE; /**< Default size of the thread pool. */
private:
thread_container m_pool = { };
std::deque<task::ptr> m_queue = { };
mutable std::mutex m_common_mutex;
std::condition_variable m_queue_not_empty_cond;
std::size_t m_free = 0;
std::size_t m_reserve = 0;
bool m_stop = false;
public:
/*!
@brief Default constructor of the thread pool.
*/
thread_pool();
/*!
@brief Constructor of the thread pool where specific size of the pool is specified.
@param[in] p_size: amount of threads in the pool that are going to be used for processing.
*/
explicit thread_pool(const std::size_t p_size);
/*!
@brief Default copy constructor of the thread pool.
*/
thread_pool(const thread_pool & p_pool) = delete;
/*!
@brief Default move constructor of the thread pool.
*/
thread_pool(thread_pool && p_pool) = delete;
/*!
@brief Default destructor of the thread pool.
*/
~thread_pool();
public:
/*!
@brief Add new task for execution to the current thread pool.
@param[in] p_raw_task: task with signature `void()` that should be executed.
@return Shared pointer to the task that is going to be executed.
*/
task::ptr add_task(const task::proc & p_raw_task);
/*!
@brief Add new task for execution to the current thread pool if there is enough capacity to serve it without delay.
@param[in] p_raw_task: task with signature `void()` that should be executed.
@return Shared pointer to the task that is going to be executed if there is enough capacity, otherwise `nullptr`.
*/
task::ptr add_task_if_free(const task::proc & p_raw_task);
/*!
@brief Returns amount of tasks in the current thread pool.
@return Amount of tasks in the current thread pool.
*/
std::size_t size() const;
private:
void initialize(const std::size_t p_size);
void get_task(task::ptr & p_task);
};
}
}
|