cppthread 1.1.16
C++ Thread Library
pool.h
Go to the documentation of this file.
1// Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#pragma once
20
33// self
34//
35#include <cppthread/exception.h>
36#include <cppthread/fifo.h>
37#include <cppthread/thread.h>
38
39
40
41namespace cppthread
42{
43
44
45
46template<class W, class ...A>
47class pool
48{
49public:
50 typedef std::shared_ptr<pool<W, A...>> pointer_t;
51 typedef typename W::work_load_type work_load_type;
53
54private:
56 {
57 public:
58 typedef std::shared_ptr<worker_thread_t> pointer_t;
59 typedef std::vector<pointer_t> vector_t;
60
61 worker_thread_t(std::string const & name
62 , std::size_t i
63 , typename worker_fifo_t::pointer_t in
64 , typename worker_fifo_t::pointer_t out
65 , A... args)
66 : f_worker(name + " (worker #" + std::to_string(i) + ")"
67 , i
68 , in
69 , out
70 , args...)
71 , f_thread(std::make_shared<thread>(name, &f_worker))
72 {
73 f_thread->start();
74 }
75
77 {
78 return f_worker;
79 }
80
81 W const & get_worker() const
82 {
83 return f_worker;
84 }
85
86 private:
87 W f_worker; // runner before thread; this is safe
89 };
90
91
92public:
94 std::string const & name
95 , std::size_t pool_size
96 , typename worker_fifo_t::pointer_t in
97 , typename worker_fifo_t::pointer_t out
98 , A... args)
99 : f_name(name)
100 , f_in(in)
101 , f_out(out)
102 {
103 if(pool_size == 0)
104 {
105 throw out_of_range("the pool size must be a positive number (1 or more).");
106 }
107 if(pool_size > 1'000)
108 {
109 throw out_of_range("pool size too large (we accept up to 1000 at this time, which is already very very large).");
110 }
111 f_workers.reserve(pool_size);
112 for(std::size_t i(0); i < pool_size; ++i)
113 {
114 f_workers.push_back(std::make_shared<worker_thread_t>(
115 f_name
116 , i
117 , f_in
118 , f_out
119 , args...));
120 }
121 }
122
124 {
125 stop(false);
126 wait();
127 }
128
129 std::size_t size() const
130 {
131 return f_workers.size();
132 }
133
134 W & get_worker(int i)
135 {
136 if(static_cast<std::size_t>(i) >= f_workers.size())
137 {
138 throw out_of_range("cppthread::pool::get_worker() called with an index out of bounds.");
139 }
140 return f_workers[i]->get_worker();
141 }
142
143 W const & get_worker(int i) const
144 {
145 if(static_cast<std::size_t>(i) >= f_workers.size())
146 {
147 throw out_of_range("cppthread::pool::get_worker() const called with an index out of bounds.");
148 }
149 return f_workers[i]->get_worker();
150 }
151
152 void push_back(work_load_type const & v)
153 {
154 f_in->push_back(v);
155 }
156
157 bool pop_front(work_load_type & v, std::int64_t usecs)
158 {
159 if(f_in->is_done())
160 {
161 usecs = 0;
162 }
163 if(f_out != nullptr)
164 {
165 return f_out->pop_front(v, usecs);
166 }
167 return false;
168 }
169
170 void stop(bool immediate)
171 {
172 if(!f_in->is_done())
173 {
174 f_in->done(immediate);
175 }
176 }
177
178 void wait()
179 {
180 f_workers.clear();
181 }
182
183
184private:
186
187 std::string const f_name;
191};
192
193
194
195} // namespace cppthread
196// vim: ts=4 sw=4 et
Create a thread safe FIFO.
Definition fifo.h:59
std::shared_ptr< fifo_type > pointer_t
A smart pointer to the FIFO.
Definition fifo.h:170
Class used to manage the worker and worker thread.
Definition pool.h:56
W & get_worker()
Retrieve a pointer to the working in this worker thread.
Definition pool.h:76
W const & get_worker() const
Retrieve the worker when the worker thread is constant.
Definition pool.h:81
std::shared_ptr< worker_thread_t > pointer_t
The shared pointer type to a worker thread.
Definition pool.h:58
W f_worker
The worker, which is a runner.
Definition pool.h:87
worker_thread_t(std::string const &name, std::size_t i, typename worker_fifo_t::pointer_t in, typename worker_fifo_t::pointer_t out, A... args)
The constructor of a worker thread.
Definition pool.h:61
thread::pointer_t f_thread
The thread which manages the worker.
Definition pool.h:88
std::vector< pointer_t > vector_t
The vector of shared pointers.
Definition pool.h:59
Manage a pool of worker threads.
Definition pool.h:48
std::string const f_name
The name of this pool of threads.
Definition pool.h:187
worker_fifo_t::pointer_t f_in
The input FIFO.
Definition pool.h:188
worker_thread_t::vector_t workers_t
Vector of workers.
Definition pool.h:185
W & get_worker(int i)
Get worker at index i.
Definition pool.h:134
void wait()
Wait on the threads to be done.
Definition pool.h:178
std::shared_ptr< pool< W, A... > > pointer_t
A shared pointer for your pools.
Definition pool.h:50
W const & get_worker(int i) const
Get worker at index i (constant version).
Definition pool.h:143
worker_fifo_t::pointer_t f_out
The output FIFO.
Definition pool.h:189
fifo< work_load_type > worker_fifo_t
This type represents the type of the fifo used by the pool.
Definition pool.h:52
~pool()
Make sure that the thread pool is cleaned up.
Definition pool.h:123
W::work_load_type work_load_type
The type of the workload item.
Definition pool.h:51
pool(std::string const &name, std::size_t pool_size, typename worker_fifo_t::pointer_t in, typename worker_fifo_t::pointer_t out, A... args)
Initializes a pool of worker threads.
Definition pool.h:93
void push_back(work_load_type const &v)
Push one work load of data.
Definition pool.h:152
std::size_t size() const
Retrieve the number of workers.
Definition pool.h:129
bool pop_front(work_load_type &v, std::int64_t usecs)
Retrieve one work load of processed data.
Definition pool.h:157
void stop(bool immediate)
Stop the threads.
Definition pool.h:170
workers_t f_workers
The vector of workers.
Definition pool.h:190
A thread object that ensures proper usage of system threads.
Definition thread.h:64
std::shared_ptr< thread > pointer_t
The shared pointer for a thread object.
Definition thread.h:66
Exceptions for the thread environment.
Thread Runner and Managers.
std::string to_string(log_level_t level)
Convert a log level to a string.
Definition log.cpp:610
Thread Runner and Managers.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.