1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! A concurrent object pool.
//!
//! # Features
//!
//! - Configurable capacity and preallocation.
//! - Thread-safe: Multiple threads can pull and recycle items concurrently.
//! - Automatic reclamation of unused item when the continuous occurrence
//! of `surplus-pull` reaches a certain threshold if `auto_reclaim` is enabled.
//!
//! # `surplus-pull`
//!
//! After pulling data from the memory pool, available allocated entities in the memory
//! pool are exceed a certain threshold. We call this pull is a `surplus-pull`.
//!
//! # Examples
//!
//! ## Local memory pool
//!
//! ```rust
//! use concurrent_pool::Pool;
//!
//! let pool: Pool<u32> = Pool::with_capacity(10);
//! assert_eq!(pool.available(), 10);
//! let item = pool.pull().unwrap();
//! assert_eq!(*item, 0);
//! assert_eq!(pool.available(), 9);
//! let item_clone = item.clone();
//! drop(item);
//! assert_eq!(pool.available(), 9);
//! drop(item_clone);
//! assert_eq!(pool.available(), 10);
//! ```
//!
//! ## Multiple threads shared memory pool
//!
//! ```rust
//! use concurrent_pool::Pool;
//! use std::sync::{Arc, mpsc};
//!
//! let pool: Arc<Pool<u32>> = Arc::new(Pool::with_capacity(10));
//!
//! let (tx, rx) = mpsc::channel();
//! let clone_pool = pool.clone();
//! let tx1 = tx.clone();
//! let sender1 = std::thread::spawn(move || {
//! let item = clone_pool.pull_owned_with(|x| *x = 1).unwrap();
//! tx1.send((1, item)).unwrap();
//! });
//!
//! let clone_pool = pool.clone();
//! let sender2 = std::thread::spawn(move || {
//! let item = clone_pool.pull_owned_with(|x| *x = 2).unwrap();
//! tx.send((2, item)).unwrap();
//! });
//!
//! let receiver = std::thread::spawn(move || {
//! for _ in 0..2 {
//! let (id, item) = rx.recv().unwrap();
//! if id == 1 {
//! assert_eq!(*item, 1);
//! } else {
//! assert_eq!(*item, 2);
//! }
//! }
//! });
//!
//! sender1.join().unwrap();
//! sender2.join().unwrap();
//! receiver.join().unwrap();
//! ```
pub use Builder;
pub use ;
pub use ;