30template <
class T, std::
size_t t_queue_size>
32 unsigned int instance_id,
35 m_tasks(psi_params.memory_resource),
36 m_workers_num(thread_num),
37 m_instance_id(instance_id),
41 m_end_execution.emplace(std::piecewise_construct, std::forward_as_tuple(i),
42 std::forward_as_tuple(
false));
46template <
class T, std::
size_t t_queue_size>
48 if (m_initialized)
return false;
50 for (
auto &[
_, end_execution] : m_end_execution) {
51 end_execution.store(
false);
53 for (
unsigned int i = 0; i < m_workers_num; i++) {
57 if (has_creation_error(worker)) {
61 m_workers.push_back(std::move(worker));
62 }
catch (
const std::system_error &) {
71template <
class T, std::
size_t t_queue_size>
74 for (
auto &worker : m_workers) {
75 if (worker.joinable()) {
80 m_initialized =
false;
83template <
class T, std::
size_t t_queue_size>
85 m_tasks.enqueue(std::move(task));
88template <
class T, std::
size_t t_queue_size>
90 return m_tasks.print_queue_state();
93template <
class T, std::
size_t t_queue_size>
95 return m_tasks.size();
98template <
class T, std::
size_t t_queue_size>
100 [[maybe_unused]]
unsigned int thread_id) {
104 auto &task_time_stat =
106 auto &worker_time_stat =
113 auto stop_waiting = [
this, &
thread_id]() ->
bool {
114 return m_end_execution[
thread_id].load();
116 auto [task, valid] = m_tasks.dequeue(stop_waiting);
130template <
class T, std::
size_t t_queue_size>
135template <
class T, std::
size_t t_queue_size>
137 for (
unsigned int i = 0; i < m_workers_num; i++) {
138 m_end_execution[i].store(
true);
140 m_tasks.notify_all();
143template <
class T, std::
size_t t_queue_size>
145 return m_workers.size();
148template <
class T, std::
size_t t_queue_size>
150#ifdef STANDALONE_LIBS_MYSQL
static mysql_service_status_t deinit()
Component deinitialization.
Definition: audit_api_message_emit.cc:575
Allocator using a Memory_resource to do the allocation.
Definition: allocator.h:52
Wrapper to mysql thread, which matches interface of std::thread.
Definition: thread_srv.h:46
int creation_error_code() const
Returns the thread creation error code.
Definition: thread_srv.h:76
static constexpr auto thp_thread_internal_id
Definition: statistics_map.h:49
static constexpr auto thp_worker_exec_time
Definition: statistics_map.h:47
static constexpr auto thp_task_exec_time
Definition: statistics_map.h:46
static Statistics_instance_monitor & get(std::size_t instance_id)
Definition: statistics_monitor.cpp:35
void end_execution()
Notifies all threads to end execution in a gracious way.
Definition: thread_pool_impl.hpp:136
bool init()
Initializes the thread pool by starting worker threads.
Definition: thread_pool_impl.hpp:47
void deinit()
Deinitializes the thread pool and joins all worker threads.
Definition: thread_pool_impl.hpp:72
unsigned int m_workers_num
The number of threads in the thread pool.
Definition: thread_pool.h:125
void run_worker(unsigned int thread_id)
Function executed by each thread.
Definition: thread_pool_impl.hpp:99
std::vector< Thread, Thread_allocator > m_workers
Thread container.
Definition: thread_pool.h:118
Thread_pool(unsigned int thread_num=std::thread::hardware_concurrency(), unsigned int instance_id=0, Thread_pool_psi psi_params={})
Constructs a thread pool.
Definition: thread_pool_impl.hpp:31
std::size_t size() const
Obtains worker pool size.
Definition: thread_pool_impl.hpp:144
std::size_t queue_size() const
Get an estimation of number of elements in the worker queue.
Definition: thread_pool_impl.hpp:94
std::unordered_map< unsigned int, std::atomic< bool > > m_end_execution
Variable indicating the end of execution, allows threads to stop in a gracious way.
Definition: thread_pool.h:121
virtual ~Thread_pool()
Destructor.
Definition: thread_pool_impl.hpp:131
std::packaged_task< T(unsigned int)> Task_type
Runnable task type, future is obtained by the caller.
Definition: thread_pool.h:53
std::string print_queue_state() const
Prints worker queue state, debug function.
Definition: thread_pool_impl.hpp:89
void enqueue(Task_type &&task)
Enqueues task for execution.
Definition: thread_pool_impl.hpp:84
static bool has_creation_error(const Thread &thread)
Checks whether worker thread construction failed.
Definition: thread_pool_impl.hpp:149
static my_thread_id thread_id
Definition: my_thr_init.cc:60
void set_stage(auto key)
Definition: stage.h:41
unsigned long long fetch_thread_mysql_id(std::size_t my_internal_id)
Fetches internal id, PSI id in case linked with mysqld, or internal thread id.
Definition: thread_srv.h:91
Definition: base_dependency_tracker.h:41
Thread_pool instrumentation parameters, packed into this structure to simplify construction of a Thre...
Definition: thread_pool_psi.h:38
#define MDEF_CREATE_THREAD(thread_key, callable,...)
Definition: thread_srv.h:103
#define _(str)
Definition: win_i18n.h:33