B:BD[
2.290] → [
2.290:766]
fn spawn<B, F, T>(mutex: &Arc<Mutex<B>>, f: F) -> JoinHandle<()>
where
B: BufferGet<T> + Send + 'static,
F: Fn(T) -> () + Send + 'static,
T: Send + 'static,
{
let mutex = mutex.clone();
thread::spawn(move || loop {
let state = { mutex.lock().unwrap().get() };
match state {
State::Empty => thread::park(),
State::Wait(duration) => thread::sleep(duration),
State::Ready(data) => f(data),
}
})
impl<B> DebouncerThread<B> {
fn new<F, T>(buffer: B, f: F) -> Self
where
B: BufferGet<T> + Send + 'static,
F: Fn(T) -> () + Send + 'static,
T: Send + 'static,
{
let mutex = Arc::new(Mutex::new(buffer));
let mutex_clone = mutex.clone();
let thread = thread::spawn(move || loop {
let state = { mutex_clone.lock().unwrap().get() };
match state {
State::Empty => thread::park(),
State::Wait(duration) => thread::sleep(duration),
State::Ready(data) => f(data),
}
});
Self { mutex, thread }
}