Struct bitcoin_scheduler::Scheduler
source · pub struct Scheduler { /* private fields */ }
Expand description
| Simple class for background tasks that
| should be run periodically or once “after
| a while”
|
| Usage:
|
| ———–
| @code
|
| CScheduler* s = new CScheduler();
| s->scheduleFromNow(doSomething, std::chrono::milliseconds{11}); // Assuming a: c_void doSomething() { }
| s->scheduleFromNow([=] { this->func(argument); }, std::chrono::milliseconds{3});
| std::thread* t = new std::thread(& { s->serviceQueue(); });
|
| … then at program shutdown, make sure to call stop() to clean up the thread(s) running serviceQueue:
| s->stop();
| t->join();
| delete t;
| delete s; // Must be done after thread is interrupted/joined.
|
Implementations§
source§impl Scheduler
impl Scheduler
sourcepub fn schedule_from_now(&mut self, f: SchedulerFunction, delta: Duration)
pub fn schedule_from_now(&mut self, f: SchedulerFunction, delta: Duration)
| Call f once after the delta has passed |
sourcepub fn stop(&mut self)
pub fn stop(&mut self)
| Tell any threads running serviceQueue | to stop as soon as the current task is | done |
sourcepub fn stop_when_drained(&mut self)
pub fn stop_when_drained(&mut self)
| Tell any threads running serviceQueue | to stop when there is no work left to be | done |
pub fn should_stop(&self) -> bool
sourcepub fn service_queue(&mut self)
pub fn service_queue(&mut self)
| Services the queue ‘forever’. Should | be run in a thread. |
sourcepub fn schedule(&mut self, f: SchedulerFunction, t: TimePoint)
pub fn schedule(&mut self, f: SchedulerFunction, t: TimePoint)
| Call func at/after time t |
sourcepub fn mock_forward(&mut self, delta_seconds: Duration)
pub fn mock_forward(&mut self, delta_seconds: Duration)
| Mock the scheduler to fast forward in | time. | | Iterates through items on taskQueue | and reschedules them to be delta_seconds | sooner. |
sourcepub fn schedule_every(&mut self, f: SchedulerFunction, delta: Duration)
pub fn schedule_every(&mut self, f: SchedulerFunction, delta: Duration)
| Repeat f until the scheduler is stopped. | First run is after delta has passed once. | | The timing is not exact: Every time f | is finished, it is rescheduled to run | again after delta. If you need more accurate | scheduling, don’t use this method. |
sourcepub fn get_queue_info(
&self,
first: &mut TimePoint,
last: &mut TimePoint
) -> usize
pub fn get_queue_info( &self, first: &mut TimePoint, last: &mut TimePoint ) -> usize
| Returns number of tasks waiting to be | serviced, and first and last task times |
sourcepub fn are_threads_servicing_queue(&self) -> bool
pub fn are_threads_servicing_queue(&self) -> bool
| Returns true if there are threads actively | running in serviceQueue() |