[−][src]Struct azul_core::task::Thread
A Thread
is a simple abstraction over std::thread
that allows to offload a pure
function to a different thread (essentially emulating async / await for older compilers).
Warning
Thread
panics if it goes out of scope before .block()
was called.
Implementations
impl<T> Thread<T>
[src]
pub fn new<U>(initial_data: U, callback: fn(_: U) -> T) -> Self where
T: Send + 'static,
U: Send + 'static,
[src]
T: Send + 'static,
U: Send + 'static,
Creates a new thread that spawns a certain (pure) function on a separate thread.
This is a workaround until await
is implemented. Note that invoking this function
will create an OS-level thread.
Warning: You must call .await()
, otherwise the Thread
will panic when it is dropped!
Example
fn pure_function(input: usize) -> usize { input + 1 } let thread_1 = Thread::new(5, pure_function); let thread_2 = Thread::new(10, pure_function); let thread_3 = Thread::new(20, pure_function); // thread_1, thread_2 and thread_3 run in parallel here... let result_1 = thread_1.block(); let result_2 = thread_2.block(); let result_3 = thread_3.block(); assert_eq!(result_1, Ok(6)); assert_eq!(result_2, Ok(11)); assert_eq!(result_3, Ok(21));
pub fn block(self) -> Result<T, BlockError>
[src]
Block until the internal thread has finished and return T
Trait Implementations
Auto Trait Implementations
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,