pub struct ThreadManager { /* private fields */ }
Expand description

An abstraction for building simple multi-threaded applications.

It’s goal is to simplify the process of setting up an application composed of a fixed number of threads, adding in support for common tasks such as detecting panics and waiting for some threads to finish.

It achieves this by focusing on a narrow use-case:

  • The application has a fixed number of threads.
  • The threads are all started at the same time during initialization.
  • A panic in any thread is expected to trigger a shut down of the application.
  • Some threads finishing should trigger a shut down of the application.

For example, a web application has a main server thread and a metrics/stats server thread.

ThreadManager is a higher level abstraction built on top of ThreadMonitor, which is used to detect and handle panics in other threads. ThreadMonitor can be used directly if ThreadManager does not meet a use-case or low level control is needed.

Limitations:

  • Only threads spawned directly through the manager interface can be monitored. The manager doesn’t monitor other threads spawned separately, even if it’s done inside one of the original spawned threads.
  • The return type of the JoinHandle is expected to be ().

Implementations§

source§

impl ThreadManager

source

pub fn new(monitor: &'static ThreadMonitor) -> ThreadManager

Instantiates a new manager.

A static reference to a ThreadMonitor that has already been initialized is expected.

See the documentation of ThreadMonitor::init for more details.

source

pub fn spawn<F>(&mut self, f: F, triggers_shutdown: bool) -> Threadwhere
    F: FnOnce() + Send + 'static,

Spawns a new thread to be managed.

Unlike std::thread::spawn, a copy of the Thread will be returned instead of a std::thread::JoinHandle. The handle will be used internally by the manager.

source

pub fn join_all(self) -> Result<FinishReason, Error>

Waits for any thread marked as triggers_shutdown to finish or for one or more threads to panic.

A FinishReason will be returned providing context into why the manager returned.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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 Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
    S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more