Skip to content

Instantly share code, notes, and snippets.

@drbh
Created October 28, 2019 15:38
Show Gist options
  • Save drbh/255486725f0bbcd7fe12dcb49391539b to your computer and use it in GitHub Desktop.
Save drbh/255486725f0bbcd7fe12dcb49391539b to your computer and use it in GitHub Desktop.
A simple mutex POOL for a custom obj. Useful for templating data storage connector polls in a multi threaded way
#![feature(drop_types_in_const)]
use std::sync::{Mutex, MutexGuard};
#[macro_use]
extern crate lazy_static;
lazy_static! {
static ref POOL: Mutex<Vec<Obj>> = Mutex::new(vec![]);
}
pub struct Obj;
fn get_pool<'a>() -> MutexGuard<'a, Vec<Obj>> {
POOL.lock().unwrap()
}
impl Drop for Obj {
fn drop(&mut self) {
println!("dropping.");
// println!("hangs here...");
get_pool().push(Obj {});
// println!("not here...");
}
}
impl Obj {
pub fn new() -> Obj {
println!("initializing");
let o = get_pool().pop();
o.unwrap_or(Obj {})
}
}
fn main() {
Obj::new();
Obj::new();
println!("Now reaches this point.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment