libmoost
moost::thread::async_worker< TWork > Class Template Reference

async_worker is a virtual class that simplifies the mechanics of doing work asynchronously. More...

#include <async_worker.hpp>

Inheritance diagram for moost::thread::async_worker< TWork >:

List of all members.

Public Member Functions

 async_worker (size_t num_threads=1, size_t max_queue=0, size_t enqueue_timeout_ms=0)
 Constructs an async_worker.
virtual ~async_worker ()
void enqueue (const TWork &work)
 Enqueues some work, and wakes up worker threads as necessary.
void start ()
void stop ()

Protected Member Functions

virtual void do_work (TWork &work)=0
 Override this method and do your work here.
virtual void report_error (const std::exception &)
 Optionally override this method for custom error logging.

Private Member Functions

void work_loop ()
 Entry point for worker threads.

Private Attributes

std::vector< boost::shared_ptr
< boost::thread > > 
m_pworker_threads
std::vector< boost::shared_ptr
< TWork > > 
m_work
size_t m_max_queue
size_t m_enqueue_timeout_ms
bool m_working
boost::mutex m_work_mutex
boost::condition m_work_to_do
boost::condition m_work_done

Detailed Description

template<typename TWork>
class moost::thread::async_worker< TWork >

async_worker is a virtual class that simplifies the mechanics of doing work asynchronously.

To use it, you must inherit from it and provide a template parameter, then implement the do_work method.

Definition at line 54 of file async_worker.hpp.


Constructor & Destructor Documentation

template<typename TWork>
moost::thread::async_worker< TWork >::async_worker ( size_t  num_threads = 1,
size_t  max_queue = 0,
size_t  enqueue_timeout_ms = 0 
) [inline]

Constructs an async_worker.

Parameters:
num_threadsthe number of worker threads to launch
max_queuethe maximum length the queue may grow before further enqueue's begin to wait (0 means allow infinite queue lengths)
enqueue_timeoutthe longest amount of time (ms) an enqueue may wait (0 means wait forever)

Definition at line 123 of file async_worker.hpp.

template<typename TWork>
virtual moost::thread::async_worker< TWork >::~async_worker ( ) [inline, virtual]

destroys async_worker

note: it's unsafe to destroy an async worker that's running!

Definition at line 139 of file async_worker.hpp.


Member Function Documentation

template<typename TWork>
virtual void moost::thread::async_worker< TWork >::do_work ( TWork &  work) [protected, pure virtual]

Override this method and do your work here.

Implemented in moost::io::async_writer< TWork, TRolloverPolicy >, TimeoutAsyncWorker, and SimpleAsyncWorker.

Here is the caller graph for this function:

template<typename TWork>
void moost::thread::async_worker< TWork >::enqueue ( const TWork &  work) [inline]

Enqueues some work, and wakes up worker threads as necessary.

Parameters:
workis the work to be done

Definition at line 152 of file async_worker.hpp.

Here is the caller graph for this function:

template<typename TWork>
virtual void moost::thread::async_worker< TWork >::report_error ( const std::exception &  ) [inline, protected, virtual]

Optionally override this method for custom error logging.

Definition at line 114 of file async_worker.hpp.

Here is the caller graph for this function:

template<typename TWork>
void moost::thread::async_worker< TWork >::start ( ) [inline]

Definition at line 183 of file async_worker.hpp.

Here is the caller graph for this function:

template<typename TWork>
void moost::thread::async_worker< TWork >::stop ( ) [inline]

Reimplemented in moost::io::async_writer< TWork, TRolloverPolicy >.

Definition at line 196 of file async_worker.hpp.

template<typename TWork>
void moost::thread::async_worker< TWork >::work_loop ( ) [inline, private]

Entry point for worker threads.

Definition at line 69 of file async_worker.hpp.

Here is the caller graph for this function:


Member Data Documentation

template<typename TWork>
size_t moost::thread::async_worker< TWork >::m_enqueue_timeout_ms [private]

Definition at line 61 of file async_worker.hpp.

template<typename TWork>
size_t moost::thread::async_worker< TWork >::m_max_queue [private]

Definition at line 60 of file async_worker.hpp.

template<typename TWork>
std::vector< boost::shared_ptr<boost::thread> > moost::thread::async_worker< TWork >::m_pworker_threads [private]

Definition at line 58 of file async_worker.hpp.

template<typename TWork>
std::vector< boost::shared_ptr< TWork > > moost::thread::async_worker< TWork >::m_work [private]

Definition at line 59 of file async_worker.hpp.

template<typename TWork>
boost::condition moost::thread::async_worker< TWork >::m_work_done [private]

Definition at line 66 of file async_worker.hpp.

template<typename TWork>
boost::mutex moost::thread::async_worker< TWork >::m_work_mutex [private]

Definition at line 64 of file async_worker.hpp.

template<typename TWork>
boost::condition moost::thread::async_worker< TWork >::m_work_to_do [private]

Definition at line 65 of file async_worker.hpp.

template<typename TWork>
bool moost::thread::async_worker< TWork >::m_working [private]

Definition at line 62 of file async_worker.hpp.


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