MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
mongo::BackgroundJob Class Referenceabstract

Background thread dispatching. More...

#include <background.h>

+ Inheritance diagram for mongo::BackgroundJob:

Public Member Functions

void go ()
 starts job.
 
Status cancel ()
 If the job has not yet started, transitions the job to the 'done' state immediately, such that subsequent calls to 'go' are ignored, and notifies any waiters waiting in 'wait'.
 
bool wait (unsigned msTimeOut=0)
 wait for completion.
 

Protected Member Functions

 BackgroundJob (bool selfDelete=false)
 sub-class must instantiate the BackgroundJob
 
virtual void run ()=0
 define this to do your work.
 

Detailed Description

Background thread dispatching.

subclass and define run()

It is not possible to run the job more than once. An attempt to call 'go' while the task is running will fail. Calling 'go' after the task has finished are ignored and will not start the job again.

Thread safety: Note that when the 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.

The overridden run() method will be executed on the background thread, so the BackgroundJob object must exist for as long the background thread is running.

Constructor & Destructor Documentation

◆ BackgroundJob()

mongo::BackgroundJob::BackgroundJob ( bool  selfDelete = false)
explicitprotected

sub-class must instantiate the BackgroundJob

Parameters
selfDeleteif set to true, object will destruct itself after the run() finished
Note
selfDelete instances cannot be wait()-ed upon

Member Function Documentation

◆ cancel()

Status mongo::BackgroundJob::cancel ( )

If the job has not yet started, transitions the job to the 'done' state immediately, such that subsequent calls to 'go' are ignored, and notifies any waiters waiting in 'wait'.

If the job has already been started, this method returns a not-ok status: it does not cancel running jobs. For this reason, you must still call 'wait' on a BackgroundJob even after calling 'cancel'.

◆ go()

void mongo::BackgroundJob::go ( )

starts job.

returns immediately after dispatching.

Note
the BackgroundJob object must live for as long the thread is still running, ie until getState() returns Done.

◆ run()

virtual void mongo::BackgroundJob::run ( )
protectedpure 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.

◆ wait()

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

wait for completion.

Parameters
msTimeOutmaximum amount of time to wait in milliseconds
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.

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