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
|
use std::{
sync::atomic::{AtomicUsize, Ordering},
thread,
};
static THREADS_RUNNING: AtomicUsize = AtomicUsize::new(0);
pub fn both_par<F1, F2, O1, O2>(f1: F1, f2: F2) -> (O1, O2)
where
F1: FnOnce() -> O1 + Send + 'static,
O1: Send + 'static,
F2: FnOnce() -> O2,
{
if THREADS_RUNNING.load(Ordering::Relaxed) < 12 {
THREADS_RUNNING.fetch_add(1, Ordering::Relaxed);
let o1h = thread::spawn(move || f1());
let o2 = f2();
let o1 = o1h.join().unwrap();
THREADS_RUNNING.fetch_sub(1, Ordering::Relaxed);
(o1, o2)
} else {
(f1(), f2())
}
}
|