Untitled
unknown
plain_text
3 years ago
874 B
5
Indexable
use std::sync::{Condvar, Mutex};
struct ExecutionLimiter {
limit: i32,
count: Mutex<i32>,
cv: Condvar
}
impl <T, R> ExecutionLimiter {
fn new(N: i32) -> Self {
return ExecutionLimiter {
limit: N,
count: Mutex::new(0),
cv: Condvar::new()
}
}
pub fn execute(&self, f: T) where T: Fn() -> R {
let mut lock = self.count.lock().unwrap();
while *lock == self.limit {
lock = self.cv.wait(lock).unwrap();
}
//incremento il counter ed eseguo la funzione
*lock += 1;
let res: R = f();
/* match res {
Err(()) => {*lock -= 1; return res;},
_ => {*lock-=1; return res;}
}*/
*lock -= 1;
self.cv.notify_all();
return res;
}
}
fn main() {
}
Editor is loading...