pub struct Scope<'scope, 'pool: 'scope> { /* private fields */ }Expand description
A scope to spawn jobs inside a ThreadPool
This struct is created by the ThreadPool::scope function
Implementations§
Source§impl<'scope, 'pool> Scope<'scope, 'pool>
impl<'scope, 'pool> Scope<'scope, 'pool>
Sourcepub fn execute(&self, job: impl Job<'scope>)
pub fn execute(&self, job: impl Job<'scope>)
Executes a job inside this Scope.
Examples found in repository?
examples/scopes.rs (lines 25-29)
8pub fn main() {
9 let conf = PoolConfig::builder().max_jobs(16).build();
10 let pool = ThreadPool::new(conf).unwrap();
11
12 let nums = (0..1000).collect::<Vec<_>>();
13
14 let n = Mutex::new(0);
15
16 fn delay() {
17 use core::hash::BuildHasher;
18 let rand = RandomState::new().build_hasher().finish();
19 let millis = rand % 500;
20 std::thread::sleep(Duration::from_millis(1000 + millis));
21 }
22
23 pool.scope(|scope| {
24 scope.subscope(|sc| {
25 sc.execute(|| {
26 delay();
27 *n.lock().unwrap() += nums.iter().sum::<usize>();
28 println!("Sum1");
29 });
30 sc.execute(|| {
31 delay();
32 *n.lock().unwrap() += nums.iter().filter(|n| *n % 2 == 0).sum::<usize>();
33 println!("Sum even");
34 });
35 });
36
37 scope.subscope(|sc| {
38 sc.execute(|| {
39 delay();
40 *n.lock().unwrap() *= nums.iter().max().unwrap();
41 println!("Mul max");
42 });
43
44 sc.execute(|| {
45 delay();
46 *n.lock().unwrap() *= nums[nums.len() / 2];
47 println!("Mul mid");
48 });
49 });
50 });
51
52 let mut expected = 0;
53 expected += nums.iter().sum::<usize>();
54 expected += nums.iter().filter(|n| *n % 2 == 0).sum::<usize>();
55 expected *= nums.iter().max().unwrap();
56 expected *= nums[nums.len() / 2];
57
58 let n = *n.lock().unwrap();
59 assert_eq!(n, expected);
60 println!("{n} == {expected}");
61}Sourcepub fn subscope<'new, F, R>(&self, f: F) -> R
pub fn subscope<'new, F, R>(&self, f: F) -> R
Creates a new scope inside self.
§Example
use job_pool::ThreadPool;
let pool = ThreadPool::default();
let msg = String::from("Helloo :)");
pool.scope(|scope| {
let helloworld = format!("{msg} world!");
scope.subscope(|subscope1| {
subscope1.execute(|| println!("1) {helloworld}"));
subscope1.execute(|| println!("2) {helloworld}"));
});
});Examples found in repository?
examples/scopes.rs (lines 24-35)
8pub fn main() {
9 let conf = PoolConfig::builder().max_jobs(16).build();
10 let pool = ThreadPool::new(conf).unwrap();
11
12 let nums = (0..1000).collect::<Vec<_>>();
13
14 let n = Mutex::new(0);
15
16 fn delay() {
17 use core::hash::BuildHasher;
18 let rand = RandomState::new().build_hasher().finish();
19 let millis = rand % 500;
20 std::thread::sleep(Duration::from_millis(1000 + millis));
21 }
22
23 pool.scope(|scope| {
24 scope.subscope(|sc| {
25 sc.execute(|| {
26 delay();
27 *n.lock().unwrap() += nums.iter().sum::<usize>();
28 println!("Sum1");
29 });
30 sc.execute(|| {
31 delay();
32 *n.lock().unwrap() += nums.iter().filter(|n| *n % 2 == 0).sum::<usize>();
33 println!("Sum even");
34 });
35 });
36
37 scope.subscope(|sc| {
38 sc.execute(|| {
39 delay();
40 *n.lock().unwrap() *= nums.iter().max().unwrap();
41 println!("Mul max");
42 });
43
44 sc.execute(|| {
45 delay();
46 *n.lock().unwrap() *= nums[nums.len() / 2];
47 println!("Mul mid");
48 });
49 });
50 });
51
52 let mut expected = 0;
53 expected += nums.iter().sum::<usize>();
54 expected += nums.iter().filter(|n| *n % 2 == 0).sum::<usize>();
55 expected *= nums.iter().max().unwrap();
56 expected *= nums[nums.len() / 2];
57
58 let n = *n.lock().unwrap();
59 assert_eq!(n, expected);
60 println!("{n} == {expected}");
61}Trait Implementations§
Auto Trait Implementations§
impl<'scope, 'pool> Freeze for Scope<'scope, 'pool>
impl<'scope, 'pool> !RefUnwindSafe for Scope<'scope, 'pool>
impl<'scope, 'pool> Send for Scope<'scope, 'pool>
impl<'scope, 'pool> Sync for Scope<'scope, 'pool>
impl<'scope, 'pool> Unpin for Scope<'scope, 'pool>
impl<'scope, 'pool> !UnwindSafe for Scope<'scope, 'pool>
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