pub struct BlockingQueue<'a, T> { /* private fields */ }Expand description
A thread safe blocking queue. Objects are added to the queue via a closure and are owned by the queue as well This means objects in the queue will only be dropped when the queue’s backing buffer is also destroyed.
Implementations§
Source§impl<'a, T> BlockingQueue<'a, T>
use rustpool::BlockingQueue;
use rustpool::RPQueue;
fn main() {
let new_value: isize = 1;
// create a blocking queue with 3 elements set to value 0
let q = BlockingQueue::<isize>::new(3, || 0);
println!("simple blocking queue size {}", q.capacity());
// take the first element
let v1 = q.take().unwrap();
println!("first element is {}", v1);
// remove the remaining items
let v2 = q.take().unwrap();
let v3 = q.take().unwrap();
// how many items available now?
println!("items in the queue {}", q.available());
// no more items to return, let's wait at most one second
let empty = q.take_wait(std::time::Duration::from_secs(1));
assert_eq!(None, empty);
// put everything back
q.offer(v1);
q.offer(v2);
q.offer(v3);
// how many items available now?
println!("items in the queue {}", q.available());
// we can't add more items than the original capacity
q.offer(&new_value);
// we still have the same number of available items
println!("items in the queue {}", q.available());
}
impl<'a, T> BlockingQueue<'a, T>
use rustpool::BlockingQueue;
use rustpool::RPQueue;
fn main() {
let new_value: isize = 1;
// create a blocking queue with 3 elements set to value 0
let q = BlockingQueue::<isize>::new(3, || 0);
println!("simple blocking queue size {}", q.capacity());
// take the first element
let v1 = q.take().unwrap();
println!("first element is {}", v1);
// remove the remaining items
let v2 = q.take().unwrap();
let v3 = q.take().unwrap();
// how many items available now?
println!("items in the queue {}", q.available());
// no more items to return, let's wait at most one second
let empty = q.take_wait(std::time::Duration::from_secs(1));
assert_eq!(None, empty);
// put everything back
q.offer(v1);
q.offer(v2);
q.offer(v3);
// how many items available now?
println!("items in the queue {}", q.available());
// we can't add more items than the original capacity
q.offer(&new_value);
// we still have the same number of available items
println!("items in the queue {}", q.available());
}Sourcepub fn new(size: usize, f: impl FnMut() -> T) -> Self
pub fn new(size: usize, f: impl FnMut() -> T) -> Self
Create a new queue with fixed size size using custom create function
Examples found in repository?
examples/blocking_queue.rs (line 9)
5fn main() {
6 let new_value: isize = 1;
7
8 // create a blocking queue with 3 elements set to value 0
9 let q = BlockingQueue::<isize>::new(3, || 0);
10
11 println!("simple blocking queue size {}", q.capacity());
12
13 // take the first element
14 let v1 = q.take().unwrap();
15
16 println!("first element is {}", v1);
17
18 // remove the remaining items
19 let v2 = q.take().unwrap();
20 let v3 = q.take().unwrap();
21
22 // how many items available now?
23 println!("items in the queue {}", q.available());
24
25 // no more items to return, let's wait at most one second
26 let empty = q.take_wait(std::time::Duration::from_secs(1));
27
28 assert_eq!(None, empty);
29
30 // put everything back
31 q.offer(v1);
32 q.offer(v2);
33 q.offer(v3);
34
35 // how many items available now?
36 println!("items in the queue {}", q.available());
37
38 // we can't add more items than the original capacity
39 q.offer(&new_value);
40
41 // we still have the same number of available items
42 println!("items in the queue {}", q.available());
43}Trait Implementations§
Source§impl<'a, T> Drop for BlockingQueue<'a, T>
impl<'a, T> Drop for BlockingQueue<'a, T>
Source§impl<'a, T> RPQueue<'a, T> for BlockingQueue<'a, T>
impl<'a, T> RPQueue<'a, T> for BlockingQueue<'a, T>
Source§fn capacity(&self) -> usize
fn capacity(&self) -> usize
Returns the size of the queue in total number objects originally allocated
Source§fn take(&self) -> Option<&'a mut T>
fn take(&self) -> Option<&'a mut T>
Returns an object from the queue or None if the pool is empty. This method allows a race when checking if
the pool is empty in order to quickly exit without causing lock contention.
This method is preferred over BlockingQueue::take_wait when empty or near empty pools are common.
impl<'a, T> Send for BlockingQueue<'a, T>
impl<'a, T> Sync for BlockingQueue<'a, T>
Auto Trait Implementations§
impl<'a, T> !Freeze for BlockingQueue<'a, T>
impl<'a, T> RefUnwindSafe for BlockingQueue<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Unpin for BlockingQueue<'a, T>
impl<'a, T> UnwindSafe for BlockingQueue<'a, T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more