Scope

Struct Scope 

Source
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>

Source

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}
Source

pub fn subscope<'new, F, R>(&self, f: F) -> R
where F: FnOnce(&Scope<'new, 'pool>) -> R, 'scope: 'new,

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§

Source§

impl Drop for Scope<'_, '_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.