Changeset View
Changeset View
Standalone View
Standalone View
src/scheduler.h
Show All 16 Lines | |||||
// | // | ||||
// Simple class for background tasks that should be run periodically or once | // Simple class for background tasks that should be run periodically or once | ||||
// "after a while" | // "after a while" | ||||
// | // | ||||
// Usage: | // Usage: | ||||
// | // | ||||
// CScheduler* s = new CScheduler(); | // CScheduler* s = new CScheduler(); | ||||
// s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { } | // s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { } | ||||
// s->scheduleFromNow(boost::bind(Class::func, this, argument), 3); | // s->scheduleFromNow(std::bind(Class::func, this, argument), 3); | ||||
// boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, | // boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, | ||||
// s)); | // s)); | ||||
// | // | ||||
// ... then at program shutdown, clean up the thread running serviceQueue: | // ... then at program shutdown, clean up the thread running serviceQueue: | ||||
// t->interrupt(); | // t->interrupt(); | ||||
// t->join(); | // t->join(); | ||||
// delete t; | // delete t; | ||||
// delete s; // Must be done after thread is interrupted/joined. | // delete s; // Must be done after thread is interrupted/joined. | ||||
// | // | ||||
class CScheduler { | class CScheduler { | ||||
public: | public: | ||||
CScheduler(); | CScheduler(); | ||||
~CScheduler(); | ~CScheduler(); | ||||
typedef std::function<void(void)> Function; | typedef std::function<void(void)> Function; | ||||
// Call func at/after time t | // Call func at/after time t | ||||
void schedule(Function f, boost::chrono::system_clock::time_point t); | void schedule(Function f, boost::chrono::system_clock::time_point t); | ||||
// Convenience method: call f once deltaSeconds from now | // Convenience method: call f once deltaMilliSeconds from now | ||||
void scheduleFromNow(Function f, int64_t deltaSeconds); | void scheduleFromNow(Function f, int64_t deltaMilliSeconds); | ||||
// Another convenience method: call f approximately every deltaSeconds | // Another convenience method: call f approximately every deltaMilliSeconds | ||||
// forever, starting deltaSeconds from now. To be more precise: every time f | // forever, starting deltaMilliSeconds from now. To be more precise: every | ||||
// is finished, it is rescheduled to run deltaSeconds later. If you need | // time f is finished, it is rescheduled to run deltaMilliSeconds later. If | ||||
// more accurate scheduling, don't use this method. | // you need more accurate scheduling, don't use this method. | ||||
void scheduleEvery(Function f, int64_t deltaSeconds); | void scheduleEvery(Function f, int64_t deltaMilliSeconds); | ||||
// To keep things as simple as possible, there is no unschedule. | // To keep things as simple as possible, there is no unschedule. | ||||
// Services the queue 'forever'. Should be run in a thread, and interrupted | // Services the queue 'forever'. Should be run in a thread, and interrupted | ||||
// using boost::interrupt_thread | // using boost::interrupt_thread | ||||
void serviceQueue(); | void serviceQueue(); | ||||
// Tell any threads running serviceQueue to stop as soon as they're done | // Tell any threads running serviceQueue to stop as soon as they're done | ||||
Show All 22 Lines |