Struct ThreadLooper

Source
pub struct ThreadLooper { /* private fields */ }
Expand description

The ThreadLooper struct is designed for managing a looping background thread.

This struct provides functionality to start, manage, and terminate a looping background thread. It is useful for tasks that need to run continuously in the background until explicitly stopped.

§Fields

  • status: An Arc<AtomicBool> indicating whether the looper is active.
  • thread: A Cell containing an Option<thread::JoinHandle<()>> for the background thread.
  • termination: An Arc<AtomicBool> indicating whether the looper should be terminated.

Implementations§

Source§

impl ThreadLooper

Source

pub fn new() -> Self

Creates a new instance of ThreadLooper.

Initializes the status and termination flags to false and sets the thread handle to None.

§Returns

A new instance of ThreadLooper.

Source

pub fn is_active(&self) -> bool

Checks if the looper is currently active.

Source

pub fn start<F>(&self, function: F)
where F: Fn() + Send + 'static,

Starts the looper with the given function.

If the looper is not already active, this method will start the background thread and run the provided function in a loop.

§Type Parameters
  • F: The type of the function to execute in the loop.
§Arguments
  • function: The function to be executed in the background thread.
Source

pub fn terminate(&self)

Terminates the background thread.

If the background thread is running, this method will signal it to stop and join the thread, effectively waiting for it to finish execution.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.