Background thread dispatching. More...
#include <background.h>
Public Types | |
| enum | State { NotStarted, Running, Done } |
Public Member Functions | |
| BackgroundJob & | go () |
| starts job. | |
| bool | wait (unsigned msTimeOut=0) |
| wait for completion. | |
| State | getState () const |
| bool | running () const |
Protected Member Functions | |
| BackgroundJob (bool selfDelete=false) | |
| sub-class must intantiate the BackgrounJob | |
| virtual string | name () const =0 |
| virtual void | run ()=0 |
| define this to do your work. | |
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
| mongo::BackgroundJob::BackgroundJob | ( | bool | selfDelete = false |
) | [explicit, protected] |
| BackgroundJob & mongo::BackgroundJob::go | ( | ) |
starts job.
returns immediatelly after dispatching.
| 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::PeriodicTask::Runner.
| bool mongo::BackgroundJob::wait | ( | unsigned | msTimeOut = 0 |
) |
wait for completion.
| msTimeOut | maximum amount of time to wait in millisecons |
1.6.3