pub struct BackgroundRunner<T> { /* private fields */ }Expand description
Runner for a background task to be invoked periodically.
Implementations§
Source§impl<T: 'static + Send> BackgroundRunner<T>
impl<T: 'static + Send> BackgroundRunner<T>
Sourcepub fn new(task: impl 'static + Send + FnMut(&T)) -> Selfwhere
T: Default,
pub fn new(task: impl 'static + Send + FnMut(&T)) -> Selfwhere
T: Default,
Create a new background runner with the given task
This spawns a new thread to execute the task periodically, triggered by the Self::update() method.
Examples found in repository?
6fn main() {
7 let mut runner_iter = 0;
8
9 // Set up the runner, giving it the task to run
10 let runner = BackgroundRunner::new(move |iter| {
11 // Simulate some heavy work
12 sleep(Duration::from_millis(10));
13 println!("runner_iter = {runner_iter}, iter = {iter}");
14 runner_iter += 1;
15 });
16
17 let start = Instant::now();
18 let mut iter = 0;
19 while start.elapsed().as_millis() < 100 {
20 // Update the runner with the current loop iteration
21 runner.update(&iter);
22 iter += 1;
23 }
24}Sourcepub fn with_init_data(
init_data: T,
task: impl 'static + Send + FnMut(&T),
) -> Self
pub fn with_init_data( init_data: T, task: impl 'static + Send + FnMut(&T), ) -> Self
Create a new background runner with the given initial data and task
This spawns a new thread to execute the task periodically, triggered by the Self::update() method.
Sourcepub fn update(&self, new_data: &T)where
T: Clone,
pub fn update(&self, new_data: &T)where
T: Clone,
Trigger the runner’s task if it’s not currently running.
Also see Self::update_with() for details.
T::clone_from() is invoked to update the data that is passed to the task.
Examples found in repository?
6fn main() {
7 let mut runner_iter = 0;
8
9 // Set up the runner, giving it the task to run
10 let runner = BackgroundRunner::new(move |iter| {
11 // Simulate some heavy work
12 sleep(Duration::from_millis(10));
13 println!("runner_iter = {runner_iter}, iter = {iter}");
14 runner_iter += 1;
15 });
16
17 let start = Instant::now();
18 let mut iter = 0;
19 while start.elapsed().as_millis() < 100 {
20 // Update the runner with the current loop iteration
21 runner.update(&iter);
22 iter += 1;
23 }
24}Sourcepub fn update_with(&self, f: impl FnOnce(&mut T))
pub fn update_with(&self, f: impl FnOnce(&mut T))
Trigger the runner’s task if it’s not currently running.
This will never block the current thread. If the task is currently running
and thus not able to process this update request, this update is dicarded
and this method returns immediately without any effect (also f is not
invoked in this case).
The given closure is invoked with a mutable reference to the currently
stored data to allow for its modification (e.g. via clone_from).
Sourcepub fn wait_and_update(&self, new_data: &T)where
T: Clone,
pub fn wait_and_update(&self, new_data: &T)where
T: Clone,
Trigger the runner’s task, blocking until it can be executed.
Also see Self::wait_and_update_with() for details.
T::clone_from() is invoked to update the data that is passed to the task.
Sourcepub fn wait_and_update_with(&self, f: impl FnOnce(&mut T))
pub fn wait_and_update_with(&self, f: impl FnOnce(&mut T))
Trigger the runner’s task, blocking until it can be executed.
Unlike Self::update_with(), this method will block the current
thread until the new_data can be handed over to the background task.
Thus, it is ensured that the task will see this or a later submitted
datum, given that is does not panic and the program is not terminated.
This function is particularly useful in conjunction with
Self::join() to ensure that the last update is processed completely.
The given closure is invoked with a mutable reference to the currently stored data to allow for its modification.
Sourcepub fn join(self)
pub fn join(self)
Wait for the background thread to finish.
This function will wait until the background thread has finished executing it’s last task and exited. This is useful to ensure that the last task has completed, in particular, before terminating the program.
Notice, if the program is terminating, without calling this method, the background thread might be terminated abruptly, possibly in the middle of executing the task.