Struct Thread

Source
pub struct Thread<T> { /* private fields */ }
Expand description

Handle to a stoppable thread.

Implementations§

Source§

impl<T> Thread<T>
where T: Send + 'static,

Source

pub fn spawn<F>(f: F) -> Thread<T>
where F: FnOnce(Arc<AtomicBool>) -> T + Send + 'static,

Spawn a new job with cancelation.

Source

pub fn join(self) -> Result<T, Box<dyn Any + Send + 'static>>

Join waits for the thread to exit then returns the return value.

Examples found in repository?
examples/simple.rs (line 13)
4fn main() {
5    // spawn our task, this creates a new OS thread.
6    let th = stoplight::spawn(|stop| {
7        while !stop.load(Ordering::Relaxed) {}
8        42
9    });
10
11    // stop() signals the thread to stop, and then join returns its return value.
12    th.stop();
13    assert_eq!(th.join().unwrap(), 42);
14}
Source

pub fn stop(&self)

Signal the Thread to stop, To wait for the thread to exit call join.

Examples found in repository?
examples/simple.rs (line 12)
4fn main() {
5    // spawn our task, this creates a new OS thread.
6    let th = stoplight::spawn(|stop| {
7        while !stop.load(Ordering::Relaxed) {}
8        42
9    });
10
11    // stop() signals the thread to stop, and then join returns its return value.
12    th.stop();
13    assert_eq!(th.join().unwrap(), 42);
14}
Source

pub fn thread(&self) -> &Thread

Extracts a handle to the underlying thread.

use stoplight;
use std::sync::atomic::{Ordering};

let th = stoplight::spawn(|stop| {
    while !stop.load(Ordering::Relaxed) {}
    42
});

let thread = th.thread();
eprintln!("thread id: {:?}", thread.id());

Auto Trait Implementations§

§

impl<T> Freeze for Thread<T>

§

impl<T> !RefUnwindSafe for Thread<T>

§

impl<T> Send for Thread<T>

§

impl<T> Sync for Thread<T>

§

impl<T> Unpin for Thread<T>

§

impl<T> !UnwindSafe for Thread<T>

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.