diff options
author | metamuffin <metamuffin@disroot.org> | 2022-12-06 21:46:19 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-12-06 21:46:19 +0100 |
commit | 39cb075c7f58e78899be43ca9ad4d65837f53a26 (patch) | |
tree | 61f11a221ea2d60b8dc92c1af736c0fb3c9aad2d /evc/src/threading.rs | |
parent | 437e092985e5633eee50874c337ccbdd0b76ff1e (diff) | |
download | video-codec-experiments-39cb075c7f58e78899be43ca9ad4d65837f53a26.tar video-codec-experiments-39cb075c7f58e78899be43ca9ad4d65837f53a26.tar.bz2 video-codec-experiments-39cb075c7f58e78899be43ca9ad4d65837f53a26.tar.zst |
unsafe multithreading
Diffstat (limited to 'evc/src/threading.rs')
-rw-r--r-- | evc/src/threading.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/evc/src/threading.rs b/evc/src/threading.rs new file mode 100644 index 0000000..012b5bf --- /dev/null +++ b/evc/src/threading.rs @@ -0,0 +1,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()) + } +} |