|
cppthread 1.1.16
C++ Thread Library
|
Manage a pool of worker threads. More...
Classes | |
| class | worker_thread_t |
| Class used to manage the worker and worker thread. More... | |
Public Types | |
| typedef std::shared_ptr< pool< W, A... > > | pointer_t |
| A shared pointer for your pools. | |
| typedef W::work_load_type | work_load_type |
| The type of the workload item. | |
| typedef fifo< work_load_type > | worker_fifo_t |
| This type represents the type of the fifo used by the pool. | |
Public Member Functions | |
| 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. | |
| ~pool () | |
| Make sure that the thread pool is cleaned up. | |
| W & | get_worker (int i) |
Get worker at index i. | |
| W const & | get_worker (int i) const |
Get worker at index i (constant version). | |
| bool | pop_front (work_load_type &v, std::int64_t usecs) |
| Retrieve one work load of processed data. | |
| void | push_back (work_load_type const &v) |
| Push one work load of data. | |
| std::size_t | size () const |
| Retrieve the number of workers. | |
| void | stop (bool immediate) |
| Stop the threads. | |
| void | wait () |
| Wait on the threads to be done. | |
Private Types | |
| typedef worker_thread_t::vector_t | workers_t |
| Vector of workers. | |
Private Attributes | |
| worker_fifo_t::pointer_t | f_in |
| The input FIFO. | |
| std::string const | f_name |
| The name of this pool of threads. | |
| worker_fifo_t::pointer_t | f_out |
| The output FIFO. | |
| workers_t | f_workers = workers_t() |
| The vector of workers. | |
This function manages a pool of worker threads. It allocates the threads, accepts incoming data, and returns outgoing data. Everything else is managed for you.
To make use of this template, you need to overload the snap_worker template and implement your own snap_worker::do_work() function.
Something like this:
You can do all the push_data() you need before doing any pop_front(). You may also want to consider looping with both interleaved or even have two threads: one feeder which does the push_data() and one consumer which does the pop_front().
Note that the thread pool does not guarantee order of processing. You must make sure that each individual chunk of data you pass in the push_front() function can be processed in any order.
| cppthread::pool< W, A >::pointer_t |
| cppthread::pool< W, A >::work_load_type |
Items that we add to the input and output FIFOs must be of that type.
The push_back() and pop_front() functions make use of items of this type to add items to the input fifo and pop items from the output fifo.
Note that the output fifo can be set to a null pointer. In that case the pop function cannot be used.
| cppthread::pool< W, A >::worker_fifo_t |
|
private |
|
inline |
This constructor is used to initialize the specified number of worker threads in this pool.
At this time, all the worker threads get allocated upfront.
pool_size parameter would then become a max_pool_size.| [in] | name | The name of the pool. |
| [in] | pool_size | The number of threads to create. |
| [in] | in | The input FIFO (where workers receive workload.) |
| [in] | out | The output FIFO (where finished work is sent.) |
| [in] | args | Extra arguments to initialize the workers. |
Definition at line 93 of file pool.h.
References cppthread::pool< W, A >::f_in, cppthread::pool< W, A >::f_name, cppthread::pool< W, A >::f_out, and cppthread::pool< W, A >::f_workers.
|
inline |
The destructor of the snap thread pool stops all the threads and then waits on them. Assuming that your code doesn't loop forever, the result is that all the threads are stopped, joined, and all the incoming and outgoing data was cleared.
If you need to get the output data, then make sure to call stop(), wait(), and pop_front() until it returns false. Only then can you call the destructor.
You are safe to run that stop process in your own destructor.
Definition at line 123 of file pool.h.
References cppthread::pool< W, A >::stop(), and cppthread::pool< W, A >::wait().

|
inline |
This function returns a reference to the worker at index i.
| std::range_error | If the index is out of range (negative or larger or equal to the number of workers) then this exception is raised. |
| [in] | i | The index of the worker to retrieve. |
i. Definition at line 134 of file pool.h.
References cppthread::pool< W, A >::f_workers.
|
inline |
This function returns a reference to the worker at index i.
| std::range_error | If the index is out of range (negative or larger or equal to the number of workers) then this exception is raised. |
| [in] | i | The index of the worker to retrieve. |
i. Definition at line 143 of file pool.h.
References cppthread::pool< W, A >::f_workers.
|
inline |
This function retrieves one T object from the output FIFO.
The usecs parameter can be set to -1 to wait until output is received. Set it to 0 to avoid waiting (check for data, if nothing return immediately) and a positive number to wait up to that many microseconds.
Make sure to check the function return value. If false, the v parameter is unmodified.
Once you called the stop() function, the pop_front() function can still be called until the out queue is emptied. The proper sequence is to
| [in] | v | A reference where the object gets copied. |
| [in] | usecs | The number of microseconds to wait. |
Definition at line 157 of file pool.h.
References cppthread::pool< W, A >::f_in, and cppthread::pool< W, A >::f_out.
|
inline |
This function adds a work load of data to the input. One of the worker threads will automatically pick up that work and have its snap_worker::do_work() function called.
| [in] | v | The work load of data to add to this pool. |
Definition at line 152 of file pool.h.
References cppthread::pool< W, A >::f_in.
|
inline |
This function returns the number of workers this thread pool is handling. The number is at least 1 as a thread pool can't currently be empty.
Definition at line 129 of file pool.h.
References cppthread::pool< W, A >::f_workers.
|
inline |
This function is called to ask all the threads to stop.
When the immediate parameter is set to true, whatever is left in the queue gets removed. This means you are likely to get invalid or at least incomplete results in the output. It should only be used if the plan is to cancel the current work process and trash everything away.
After a call to the stop() function, you may want to retrieve the last bits of data with the pop_front() function until it returns false and then call the join() function to wait on all the threads and call pop_front() again to make sure that you got all the output.
You may also call the join() function right after stop() and then the pop_front().
| [in] | immediate | Whether to clear the remaining items on the queue. |
Definition at line 170 of file pool.h.
References cppthread::pool< W, A >::f_in.
Referenced by cppthread::pool< W, A >::~pool().

|
inline |
This function waits on all the worker threads until they all exited. This ensures that all the output (see the pop_front() function) was generated and you can therefore end your processing.
The order of processing is as follow:
Emptying the output queue between the stop() and wait() is not required. It may always be empty or you will anyway get only a few small chunks that can wait in the buffer.
Definition at line 178 of file pool.h.
References cppthread::pool< W, A >::f_workers.
Referenced by cppthread::pool< W, A >::~pool().

|
private |
This FIFO is where work for the thread workers managed by the pool object are saved until a thread is available to process them.
The input FIFO must be a valid pointer to a fifo object.
Definition at line 188 of file pool.h.
Referenced by cppthread::pool< W, A >::pool(), cppthread::pool< W, A >::pop_front(), cppthread::pool< W, A >::push_back(), and cppthread::pool< W, A >::stop().
|
private |
You can give your pools a name so that way they can easily be tracked in your debug process. The name is not otherwise used internally.
Definition at line 187 of file pool.h.
Referenced by cppthread::pool< W, A >::pool().
|
private |
A pointer to a FIFO to output the resulting workloads. This pointer may be nullptr.
Definition at line 189 of file pool.h.
Referenced by cppthread::pool< W, A >::pool(), and cppthread::pool< W, A >::pop_front().
|
private |
The pool manages a set of workers saved in a vector. This variable holds these workers.
Definition at line 190 of file pool.h.
Referenced by cppthread::pool< W, A >::pool(), cppthread::pool< W, A >::get_worker(), cppthread::pool< W, A >::get_worker(), cppthread::pool< W, A >::size(), and cppthread::pool< W, A >::wait().
This document is part of the Snap! Websites Project.
Copyright by Made to Order Software Corp.