A pool of worker threads. More...
#include <pool.h>
Classes | |
class | Thread |
Base class for threads that can be kept in a Dv::Thread::Pool. More... | |
Public Types | |
enum | Status { NONE = 0, INITIALIZED = 1, FINALIZED = 2 } |
Pool status. More... | |
Public Member Functions | |
bool | delegate (time_t msecs=0) |
Delegate some work to an available worker thread. | |
size_t | slack () const |
Status | status () const |
size_t | size () const |
Private Member Functions | |
Pool (size_t max_threads, Factory &factory, const std::string &name) | |
Constructor. | |
~Pool () | |
Destructor. | |
const std::string & | name () const |
bool | initialize (time_t millisecs) |
Initialize a Dv::Thread::Pool. | |
bool | finalize (time_t millisecs) |
Finalize a Dv::Thread::Pool by killing all its threads (and joining them). | |
void | add_live (int n) |
Called at the beginning and the end of a thread's main function. | |
void | add_free (Thread *t) |
Pool (const Pool &) | |
Pool & | operator= (const Pool &) |
No assignment operator. | |
Private Attributes | |
Factory & | factory_ |
Factory to which the pool belongs. | |
std::vector< Thread * > | all_ |
Vector of all threads in the pool. | |
std::list< Thread * > | free_ |
List of threads waiting for work. | |
Monitor | free_monitor_ |
Monitor to use for controlling access to free_. | |
std::string | name_ |
Name of the pool. | |
size_t | live_threads_ |
Number of threads that have started their main function and not yet finished it. | |
Status | status_ |
Friends | |
class | Factory |
class | Thread |
A pool of worker threads.
Normally, a pool is owned by a Dv::Thread::Pool::Factory.
Pool status.
Dv::Thread::Pool::Pool | ( | size_t | max_threads, | |
Factory & | factory, | |||
const std::string & | name | |||
) | [private] |
Constructor.
Note that the actual initialisation of the threads in the pool is done by Dv::Thread::Pool::initialize, which is not called by this constructor. It should be called by the constructor of the user's class derived from Dv::Thread::Factory.
max_threads | number of threads that will be kept in the pool. | |
factory | object that is capable of creating a new thread | |
name | for the pool's monitor. |
Dv::Thread::Pool::~Pool | ( | ) | [private] |
Destructor.
Note that the actual finalisation of the threads in the pool is done by Dv::Thread::Pool::finalize, which is not called by this destructor. It should have been called by the destructor of the user's class derived from Dv::Thread::Factory.
Dv::Thread::Pool::Pool | ( | const Pool & | ) | [private] |
bool Dv::Thread::Pool::delegate | ( | time_t | msecs = 0 |
) |
Delegate some work to an available worker thread.
If a thread is available, this function will among other call its Dv::Thread::Pool::Thread::setup function to supply it with input and then signal it.
msecs | we are prepared to wait until a thread is ready for work (so 0 means immediately return false if there is no thread waiting for work). If negative, we will wait indefinitely until a worker thread becomes available. |
size_t Dv::Thread::Pool::slack | ( | ) | const [inline] |
References free_.
Referenced by Dv::Thread::Factory::slack().
size_t Dv::Thread::Pool::size | ( | ) | const [inline] |
References all_.
Referenced by Dv::Thread::Factory::size().
const std::string& Dv::Thread::Pool::name | ( | ) | const [inline, private] |
Reimplemented from Dv::Thread::Monitor.
References name_.
bool Dv::Thread::Pool::initialize | ( | time_t | millisecs | ) | [private] |
Initialize a Dv::Thread::Pool.
This is separate from the constructor because the function creates and starts a number of threads using the pool's Dv::Thread::Factory::create pure virtual function which only becomes available in the constructor of the Dv::Thread::Factory-derived user class.
millisecs | timeout after which it is assumed that the initialization failed. |
Referenced by Dv::Thread::Factory::initialize().
bool Dv::Thread::Pool::finalize | ( | time_t | millisecs | ) | [private] |
Finalize a Dv::Thread::Pool by killing all its threads (and joining them).
This is separate from the destructor because the function kills a number of threads which involves the pool's Dv::Thread::Factory::finalize pure virtual function which only remains available in the destructor of the Dv::Thread::Factory-derived user class.
millisecs | timeout after which it is assumed that the initialization failed. |
Referenced by Dv::Thread::Factory::finalize().
void Dv::Thread::Pool::add_live | ( | int | n | ) | [private] |
Called at the beginning and the end of a thread's main function.
If the total number of live threads becomes 0, the DEAD condition of the Pool's monitor will be signaled. Conversely, if the total number of live threads becomes equal to the capacity of the pool, the LIVE condition will be signaled.
n | whether a thread started (+1) or finished (-1) |
void Dv::Thread::Pool::add_free | ( | Thread * | t | ) | [private] |
No assignment operator.
Reimplemented from Dv::Thread::Monitor.
friend class Factory [friend] |
friend class Thread [friend] |
Factory& Dv::Thread::Pool::factory_ [private] |
Factory to which the pool belongs.
std::vector<Thread*> Dv::Thread::Pool::all_ [private] |
Vector of all threads in the pool.
Referenced by size().
std::list<Thread*> Dv::Thread::Pool::free_ [private] |
List of threads waiting for work.
Referenced by slack().
Monitor Dv::Thread::Pool::free_monitor_ [private] |
Monitor to use for controlling access to free_.
std::string Dv::Thread::Pool::name_ [private] |
size_t Dv::Thread::Pool::live_threads_ [private] |
Number of threads that have started their main function and not yet finished it.
Status Dv::Thread::Pool::status_ [private] |
Referenced by status().
dvthread-0.13.4 | [11 December, 2009] |