about
11/15/2022
RustBlockingQueue Repository
RustBlockingQueue code

RustBlockingQueue  Repository

Communicate between threads with a shared blocking queue

Quick Status Code functions correctly No known defects Demonstration code yes Documentation yes Test cases yes Static library yes Build requires Rust installation Planned design changes None at this time

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:

Fig 1. BlockingQueue
There is one struct, BlockingQueue<T>, with a few methods in this design: Methods:
  1. new() -> Self
    Create new BlockingQueue which is empty.
  2. en_q(&self, t: T) -> Result<()>
    Push_back t onto internal VecDec<T>.
  3. de_q(&self) -> Result<T>
    Pop_front t from internal VecDec<T>.
  4. 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.
  Next Prev Pages Sections About Keys