pub struct Scope<'pool, 'scope> { /* private fields */ }Available on crate feature
scope only.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§
Source§impl<'pool, 'scope> Scope<'pool, 'scope>
impl<'pool, 'scope> Scope<'pool, 'scope>
Sourcepub fn run<F: FnOnce() + Send + 'scope>(&self, f: F)
pub fn run<F: FnOnce() + Send + 'scope>(&self, f: F)
Enqueue a function that may refer to its parent scope to be executed as a job when a thread is free to do so.
Sourcepub fn run_recv<F: FnOnce() -> R + Send + 'scope, R: Send + 'scope>(
&self,
f: F,
) -> JobHandle<R>
Available on crate feature recv only.
pub fn run_recv<F: FnOnce() -> R + Send + 'scope, R: Send + 'scope>( &self, f: F, ) -> JobHandle<R>
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> Freeze for Scope<'pool, 'scope>
impl<'pool, 'scope> !RefUnwindSafe for Scope<'pool, 'scope>
impl<'pool, 'scope> Send for Scope<'pool, 'scope>
impl<'pool, 'scope> !Sync for Scope<'pool, 'scope>
impl<'pool, 'scope> Unpin for Scope<'pool, 'scope>
impl<'pool, 'scope> !UnwindSafe for Scope<'pool, 'scope>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more