1 unstable release
Uses old Rust 2015
0.1.0 | May 31, 2018 |
---|
#36 in #object-pool
7KB
139 lines
BlockingObjectPool
A thread safe, blocking, object pool in rust.
Summary
BlockingObjectPool::with_capacity
orBlockingObjectPool::with_objects
to initialize a poolpool.get
to obtain a object from the pool
If the pool is empty, get
will be blocked.
If the object is out of scope, it's been put back to the pool automatically.
That's all!
Examples
extern crate blocking_object_pool;
use blocking_object_pool::BlockingObjectPool;
#[derive(Clone)]
struct Hello {
a: i32,
b: i32,
}
fn main() {
let pool = BlockingObjectPool::with_capacity(2, || Hello { a: 1, b: 2 });
// get one object
let m = pool.get();
assert_eq!(m.a, 1);
println!("one. m.a={}", m.a);
// get another object.
{
let mut m = pool.get();
println!("two. m.a={}", m.a);
m.a = 11;
} // here the 2nd object goes out of scope, and it's been put back to the pool automatically.
// you can either call drop(m) manually.
let m = pool.get();
assert_eq!(m.a, 11);
println!("two again, m.a={}", m.a);
println!("this line will be blocked for ever");
let _m = pool.get();
}
Inspired by zslayton's lifeguard.