46template<
class W,
class ...A>
94 std::string
const & name
95 , std::size_t pool_size
105 throw out_of_range(
"the pool size must be a positive number (1 or more).");
107 if(pool_size > 1'000)
109 throw out_of_range(
"pool size too large (we accept up to 1000 at this time, which is already very very large).");
112 for(std::size_t i(0); i < pool_size; ++i)
114 f_workers.push_back(std::make_shared<worker_thread_t>(
136 if(
static_cast<std::size_t
>(i) >=
f_workers.size())
138 throw out_of_range(
"cppthread::pool::get_worker() called with an index out of bounds.");
145 if(
static_cast<std::size_t
>(i) >=
f_workers.size())
147 throw out_of_range(
"cppthread::pool::get_worker() const called with an index out of bounds.");
165 return f_out->pop_front(v, usecs);
174 f_in->done(immediate);
Create a thread safe FIFO.
std::shared_ptr< fifo_type > pointer_t
A smart pointer to the FIFO.
Class used to manage the worker and worker thread.
W & get_worker()
Retrieve a pointer to the working in this worker thread.
W const & get_worker() const
Retrieve the worker when the worker thread is constant.
std::shared_ptr< worker_thread_t > pointer_t
The shared pointer type to a worker thread.
W f_worker
The worker, which is a runner.
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.
thread::pointer_t f_thread
The thread which manages the worker.
std::vector< pointer_t > vector_t
The vector of shared pointers.
Manage a pool of worker threads.
std::string const f_name
The name of this pool of threads.
worker_fifo_t::pointer_t f_in
The input FIFO.
worker_thread_t::vector_t workers_t
Vector of workers.
W & get_worker(int i)
Get worker at index i.
void wait()
Wait on the threads to be done.
std::shared_ptr< pool< W, A... > > pointer_t
A shared pointer for your pools.
W const & get_worker(int i) const
Get worker at index i (constant version).
worker_fifo_t::pointer_t f_out
The output FIFO.
fifo< work_load_type > worker_fifo_t
This type represents the type of the fifo used by the pool.
~pool()
Make sure that the thread pool is cleaned up.
W::work_load_type work_load_type
The type of the workload item.
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.
void push_back(work_load_type const &v)
Push one work load of data.
std::size_t size() const
Retrieve the number of workers.
bool pop_front(work_load_type &v, std::int64_t usecs)
Retrieve one work load of processed data.
void stop(bool immediate)
Stop the threads.
workers_t f_workers
The vector of workers.
A thread object that ensures proper usage of system threads.
std::shared_ptr< thread > pointer_t
The shared pointer for a thread object.
Exceptions for the thread environment.
Thread Runner and Managers.
std::string to_string(log_level_t level)
Convert a log level to a string.
Thread Runner and Managers.