Struct collective::thread::manager::ThreadManager
source · 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
impl ThreadManager
sourcepub fn new(monitor: &'static ThreadMonitor) -> ThreadManager
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.
sourcepub fn spawn<F>(&mut self, f: F, triggers_shutdown: bool) -> Threadwhere
F: FnOnce() + Send + 'static,
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.
sourcepub fn join_all(self) -> Result<FinishReason, Error>
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.