1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::sync::{Arc, Condvar, Mutex};
use std::thread;

pub struct Spot {
    threads: Vec<std::thread::JoinHandle<()>>,
    working_threads_arc: Arc<(Mutex<u32>, Condvar)>,
    amount_of_threads: u32,
}

impl Spot {
    pub fn new(amount_of_threads: u32) -> Spot {
        return Spot {
            threads: Vec::new(),
            working_threads_arc: Arc::new((Mutex::new(0u32), Condvar::new())),
            amount_of_threads: amount_of_threads,
        };
    }
    pub fn post(&mut self, task: fn()) {
        let working_threads = self.working_threads_arc.clone();
        let amount_of_threads_copy = self.amount_of_threads;
        let thread = thread::spawn(move || {
            let &(ref num, ref cvar) = &*working_threads;
            {
                let mut start = num.lock().unwrap();
                while *start >= amount_of_threads_copy {
                    start = cvar.wait(start).unwrap();
                }
                *start += 1;
            }
            println!("Running!");
            task();
            println!("Done!");
            let mut start = num.lock().unwrap();
            *start -= 1;
            cvar.notify_one();
        });
        self.threads.push(thread);
    }

    pub fn end(self) {
        for thread in self.threads {
            let _result = thread.join();
        }
    }
}