Dv::Thread::Pool Class Reference

A pool of worker threads. More...

#include <pool.h>

Inheritance diagram for Dv::Thread::Pool:
Inheritance graph
[legend]
Collaboration diagram for Dv::Thread::Pool:
Collaboration graph
[legend]

List of all members.

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 &)
Pooloperator= (const Pool &)
 No assignment operator.

Private Attributes

Factoryfactory_
 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

Detailed Description

A pool of worker threads.

Normally, a pool is owned by a Dv::Thread::Pool::Factory.

See also:
Dv::Thread::Pool::Factor

Member Enumeration Documentation

Pool status.

Enumerator:
NONE 
INITIALIZED 
FINALIZED 

Constructor & Destructor Documentation

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.

Parameters:
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.
See also:
Dv::Thread::Factory::initialize
Dv::Thread::Factory::finalize
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.

See also:
Dv::Thread::Factory::finalize
Dv::Thread::Factory::initialize
Dv::Thread::Pool::Pool ( const Pool  )  [private]

Member Function Documentation

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.

Parameters:
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.
Returns:
true iff the delegation was succesful.
false iff no worker threads are available
See also:
Dv::Thread::Pool::Thread::setup
Dv::Thread::Pool::Thread::signal
size_t Dv::Thread::Pool::slack (  )  const [inline]
Returns:
the number of threads that are waiting for work.

References free_.

Referenced by Dv::Thread::Factory::slack().

Status Dv::Thread::Pool::status (  )  const [inline]
Returns:
status of pool

References status_.

size_t Dv::Thread::Pool::size (  )  const [inline]
Returns:
the number of threads in the pool.

References all_.

Referenced by Dv::Thread::Factory::size().

const std::string& Dv::Thread::Pool::name (  )  const [inline, private]
Returns:
name of this pool (used as name for its monitor and also as the basis for the names of the threads' monitor).

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.

Parameters:
millisecs timeout after which it is assumed that the initialization failed.
Returns:
true iff the initialization succeeded
See also:
Dv::Thread::Factory::init

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.

Parameters:
millisecs timeout after which it is assumed that the initialization failed.
Returns:
true iff the finalization succeeded
See also:
Dv::Thread::Factory::finit

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.

Parameters:
n whether a thread started (+1) or finished (-1)
Precondition:
-1 <= n <= +1
void Dv::Thread::Pool::add_free ( Thread t  )  [private]
Pool& Dv::Thread::Pool::operator= ( const Pool  )  [private]

No assignment operator.

Reimplemented from Dv::Thread::Monitor.


Friends And Related Function Documentation

friend class Factory [friend]
friend class Thread [friend]

Member Data Documentation

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 to use for controlling access to free_.

std::string Dv::Thread::Pool::name_ [private]

Name of the pool.

Reimplemented from Dv::Thread::Monitor.

Referenced by name().

Number of threads that have started their main function and not yet finished it.

Referenced by status().


The documentation for this class was generated from the following file:

dvthread-0.13.4 [11 December, 2009]