Created
October 28, 2019 15:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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