RepoRust_RustBlockingQueue.html
copyright © James Fawcett
Revised: 11/06/2025
Concept:
RustBlockingQueue is a facility for communicating between threads using a thread-safe blocking queue. Note that
the Rust message-passing facility does about the same thing.
This is a nice illustration of how to build a data structure that can be shared between threads. I intend to compare
performance of this facility with message passing some time soon.
Design:
There is one struct, BlockingQueue<T>, with a few methods in this design:
Methods:
-
new() -> Self
Create new BlockingQueue which is empty.
-
en_q(&self, t: T) -> Result<()>()>
Push_back t onto internal VecDec<T>.
-
de_q(&self) -> Result<T>
Pop_front t from internal VecDec<T>.
-
len(&self) -> usize
Return number of elements stored in queue.
Sharing between threads is only possible, due to rules of the Rust language, if the shared items are
all Mutexes or Condvars, or an aggregate of those, e.g., a tuple, or struct like BlockingQueue.
An instance of BlockingQueue<T> can be shared between threads because it only has two fields
and those are share-able. One is a Mutex<VecDeque<T>>, and the other is a Condvar,
e.g., a condition variable.
Operation:
Operation is illustrated by the test1.rs in /examples.
Build:
Download and, in a command prompt, cargo build or cargo run.
Status:
There may be some changes after I start building bigger Rust applications.