libmoost
|
async_worker is a virtual class that simplifies the mechanics of doing work asynchronously. More...
#include <async_worker.hpp>
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 |
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.
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.
num_threads | the number of worker threads to launch |
max_queue | the maximum length the queue may grow before further enqueue's begin to wait (0 means allow infinite queue lengths) |
enqueue_timeout | the longest amount of time (ms) an enqueue may wait (0 means wait forever) |
Definition at line 123 of file async_worker.hpp.
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.
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.
void moost::thread::async_worker< TWork >::enqueue | ( | const TWork & | work | ) | [inline] |
Enqueues some work, and wakes up worker threads as necessary.
work | is the work to be done |
Definition at line 152 of file async_worker.hpp.
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.
void moost::thread::async_worker< TWork >::start | ( | ) | [inline] |
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.
void moost::thread::async_worker< TWork >::work_loop | ( | ) | [inline, private] |
Entry point for worker threads.
Definition at line 69 of file async_worker.hpp.
size_t moost::thread::async_worker< TWork >::m_enqueue_timeout_ms [private] |
Definition at line 61 of file async_worker.hpp.
size_t moost::thread::async_worker< TWork >::m_max_queue [private] |
Definition at line 60 of file async_worker.hpp.
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.
std::vector< boost::shared_ptr< TWork > > moost::thread::async_worker< TWork >::m_work [private] |
Definition at line 59 of file async_worker.hpp.
boost::condition moost::thread::async_worker< TWork >::m_work_done [private] |
Definition at line 66 of file async_worker.hpp.
boost::mutex moost::thread::async_worker< TWork >::m_work_mutex [private] |
Definition at line 64 of file async_worker.hpp.
boost::condition moost::thread::async_worker< TWork >::m_work_to_do [private] |
Definition at line 65 of file async_worker.hpp.
bool moost::thread::async_worker< TWork >::m_working [private] |
Definition at line 62 of file async_worker.hpp.