mongo::BackgroundJob Class Reference

#include <background.h>

Inheritance diagram for mongo::BackgroundJob:
Inheritance graph
[legend]

List of all members.

Public Types

enum  State { NotStarted, Running, Done }

Public Member Functions

State getState () const
BackgroundJobgo ()
bool running () const
bool wait (unsigned msTimeOut=0)
virtual ~BackgroundJob ()

Protected Member Functions

 BackgroundJob (bool selfDelete=false)
virtual string name () const =0
virtual void run ()=0

Private Member Functions

void jobBody (boost::shared_ptr< JobStatus > status)

Private Attributes

boost::shared_ptr< JobStatus > _status

Detailed Description

Background thread dispatching. subclass and define run()

It is ok to call go(), that is, run the job, more than once -- if the previous invocation has finished. Thus one pattern of use is to embed a backgroundjob in your object and reuse it (or same thing with inheritance). Each go() call spawns a new thread.

Thread safety: note when job destructs, the thread is not terminated if still running. generally if the thread could still be running, allocate the job dynamically and set deleteSelf to true.

go() and wait() are not thread safe run() will be executed on the background thread BackgroundJob object must exist for as long the background thread is running

Definition at line 41 of file util/background.h.


Member Enumeration Documentation

Enumerator:
NotStarted 
Running 
Done 

Definition at line 66 of file util/background.h.


Constructor & Destructor Documentation

mongo::BackgroundJob::BackgroundJob ( bool  selfDelete = false  )  [explicit, protected]

sub-class must intantiate the BackgrounJob

Parameters:
selfDelete if set to true, object will destruct itself after the run() finished
Note:
selfDelete instantes cannot be wait()-ed upon
virtual mongo::BackgroundJob::~BackgroundJob (  )  [inline, virtual]

Definition at line 72 of file util/background.h.


Member Function Documentation

State mongo::BackgroundJob::getState (  )  const
BackgroundJob& mongo::BackgroundJob::go (  ) 

starts job. returns immediatelly after dispatching.

Note:
the BackgroundJob object must live for as long the thread is still running, ie until getState() returns Done.
void mongo::BackgroundJob::jobBody ( boost::shared_ptr< JobStatus >  status  )  [private]
virtual string mongo::BackgroundJob::name (  )  const [protected, pure virtual]
virtual void mongo::BackgroundJob::run (  )  [protected, pure virtual]

define this to do your work. after this returns, state is set to done. after this returns, deleted if deleteSelf true.

NOTE: if run() throws, the exception will be caught within 'this' object and will ultimately lead to the BackgroundJob's thread being finished, as if run() returned.

Implemented in mongo::ClientCursorMonitor, mongo::_MultiCommandJob, mongo::SnapshotThread, mongo::Balancer, mongo::WriteBackListener, and mongo::task::Task.

bool mongo::BackgroundJob::running (  )  const
bool mongo::BackgroundJob::wait ( unsigned  msTimeOut = 0  ) 

wait for completion.

Parameters:
msTimeOut maximum amount of time to wait in millisecons
Returns:
true if did not time out. false otherwise.
Note:
you can call wait() more than once if the first call times out. but you cannot call wait on a self-deleting job.

Member Data Documentation

boost::shared_ptr<JobStatus> mongo::BackgroundJob::_status [private]

Definition at line 99 of file util/background.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


mongodb
Author(s): Nate Koenig
autogenerated on Fri Jan 11 12:15:53 2013