thread-guard 0.1.0

A simple thread guard.
Documentation
  • Coverage
  • 100%
    7 out of 7 items documented1 out of 7 items with examples
  • Size
  • Source code size: 33.59 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 308.9 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • asynchronics/thread-guard
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • sbarral jauhien

Thread guard

This crate contains a simple thread guard.

The thread guard is used to ensure that the thread in question is always joined.

Pre-action and post-action can be defined to execute before and after thread joining, respectively. The pre-action is typically used to send a message to the thread, triggering its exit, while the post-action handles the thread's result.

In the simplest case, the guard can be used without pre-action and post-action:

use std::thread;
use thread_guard::ThreadGuard;

let guard = ThreadGuard::new(
    thread::spawn(|| {
        // Do something and exit.
    }));

The post-action can be used to handle the thread result:

use std::thread;
use thread_guard::ThreadGuard;

let guard = ThreadGuard::with_post_action(
    thread::spawn(|| {
        1
    }),
    |r| {println!("The thread exited with the result {:?}", r)});

If the thread needs to be signaled to exit, a pre-action can be used:

use std::sync::mpsc::{channel, RecvError};
use std::thread;
use thread_guard::ThreadGuard;

let (tx, rx) = channel();
let guard = ThreadGuard::with_pre_action(
    thread::spawn(move || -> Result<(), RecvError>{
        rx.recv()?;
        Ok(())
    }),
    move |_| {let _ = tx.send(());});

Finally, a pre-action may need some data that outlives the appropriate closure. An example is provided in the examples directory.