Struct ste::Thread[][src]

#[must_use =
  "The thread should be joined with Thread::join once no longer used, \
    otherwise it will block while being dropped."]pub struct Thread { /* fields omitted */ }

The handle for a background thread.

The background thread can be interacted with in a couple of ways:

  • submit - for submitted tasks, the call will block until it has been executed on the thread (or the thread has panicked).
  • drop - for dropping value on the background thread. This is necessary for Tagged values that requires drop.

Examples

use std::sync::Arc;

let thread = Arc::new(ste::Thread::new()?);
let mut threads = Vec::new();

for n in 0..10 {
    let thread = thread.clone();

    threads.push(std::thread::spawn(move || {
        thread.submit(move || n)
    }));
}

let mut result = 0;

for t in threads {
    result += t.join().unwrap()?;
}

assert_eq!(result, (0..10).sum());

// Unwrap the thread.
let thread = Arc::try_unwrap(thread).map_err(|_| "unwrap failed").unwrap();

let value = thread.submit(|| {
    panic!("Background thread: {:?}", std::thread::current().id());
});

println!("Main thread: {:?}", std::thread::current().id());
assert!(value.is_err());

assert!(thread.join().is_err());

Implementations

impl Thread[src]

pub fn new() -> Result<Self>[src]

Construct a default background thread executor.

These both do the same thing:

let thread1 = ste::Thread::new()?;
let thread2 = ste::Builder::new().build()?;

pub fn submit<F, T>(&self, task: F) -> Result<T, Panicked> where
    F: Send + FnOnce() -> T,
    T: Send
[src]

Submit a task to run on the background thread.

The call will block until it has been executed on the thread (or the thread has panicked).

Because this function blocks until completion, it can safely access values which are outside of the scope of the provided closure.

If you however need to store and access things which are !Sync, you can use Tagged.

Examples

let thread = ste::Thread::new()?;

let mut n = 10;
thread.submit(|| n += 10)?;
assert_eq!(20, n);

thread.join()?;

pub fn drop<T>(&self, value: T) -> Result<(), Panicked> where
    T: Send
[src]

Move the provided value onto the background thread and drop it.

This is necessary for Tagged values that needs to be dropped which would otherwise panic.

Examples

// Ensure that `Foo` is both `!Send` and `!Sync`.
struct Foo(*mut ());

impl Foo {
    fn test(&self) -> u32 {
        42
    }
}

impl Drop for Foo {
    fn drop(&mut self) {
        println!("Foo was dropped");
    }
}

let thread = ste::Thread::new()?;

let value = thread.submit(|| ste::Tagged::new(Foo(0 as *mut ())))?;
let out = thread.submit(|| value.test())?;
assert_eq!(42, out);

thread.drop(value)?;
thread.join()?;

If we omit the call to drop, the above will panic.

let thread = ste::Thread::new()?;

let value = thread.submit(|| ste::Tagged::new(Foo(0 as *mut ())))?;

thread.join()?;

pub fn join(self) -> Result<(), Panicked>[src]

Join the background thread.

Will block until the background thread is joined.

This is the clean way to join a background thread, the alternative is to let Thread drop and this will be performed in the drop handler instead.

Examples

let thread = ste::Thread::new()?;

let mut n = 10;
thread.submit(|| n += 10)?;
assert_eq!(20, n);

thread.join()?;

Trait Implementations

impl Drop for Thread[src]

impl Send for Thread[src]

Safety: The handle is both send and sync because it joins the background thread which keeps track of the state of shared and cleans it up once it’s no longer needed.

impl Sync for Thread[src]

Auto Trait Implementations

impl !RefUnwindSafe for Thread

impl Unpin for Thread

impl !UnwindSafe for Thread

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.