Struct lagoon::Scope [−][src]
pub struct Scope<'pool, 'scope> { /* fields omitted */ }
Expand description
A scope within which jobs that refer to their parent scope may safely be spawned.
The example below demonstrates how scoped threads can easily be used to perform safe, ergonomic parallel data
processing, similar to rayon
.
Note: This is a contrived example. The overhead of spawning jobs for each element of the vector will far outweigh the cost of each operation in this case.
// A vector of the numbers 0 to 99 let mut data = (0..100).collect::<Vec<u32>>(); lagoon::ThreadPool::default().scoped(|s| { // For each element in the vector... for x in data.iter_mut() { // ...spawn a job that squares that element s.run(move || *x *= *x); } }); // Demonstrate that the elements have indeed been squared assert!((0..100) .map(|x| x * x) .zip(data.into_iter()) .all(|(x, y)| x == y));
Implementations
Enqueue a function that may refer to its parent scope to be executed as a job when a thread is free to do so.
This is supported on crate feature recv
only.
This is supported on crate feature
recv
only.Enqueue a function that may refer to its parent scope to be executed as a job when a thread is free to do so, returning a handle that allows retrieval of the return value of the function.
Auto Trait Implementations
impl<'pool, 'scope> !RefUnwindSafe for Scope<'pool, 'scope>
impl<'pool, 'scope> !UnwindSafe for Scope<'pool, 'scope>