arc_mutex/
arc_mutex.rs

1extern crate poolite;
2use poolite::Pool;
3
4use std::collections::BTreeMap;
5use std::sync::{Arc, Mutex};
6
7/// `cargo run --example arc_mutex`
8fn main() {
9    let pool = Pool::new().unwrap();
10    // You also can use RwLock instead of Mutex if you read more than write.
11    let map = Arc::new(Mutex::new(BTreeMap::<i32, i32>::new()));
12    for i in 0..38 {
13        let map = map.clone();
14        pool.push(move || test(i, map));
15    }
16
17    pool.join(); //wait for the pool
18
19    for (k, v) in map.lock().unwrap().iter() {
20        println!("key: {}\tvalue: {}", k, v);
21    }
22}
23
24fn test(msg: i32, map: Arc<Mutex<BTreeMap<i32, i32>>>) {
25    let res = fib(msg);
26    let mut maplock = map.lock().unwrap();
27    maplock.insert(msg, res);
28}
29
30fn fib(msg: i32) -> i32 {
31    match msg {
32        0...2 => 1,
33        x => fib(x - 1) + fib(x - 2),
34    }
35}