about
11/15/2022
RustThreadPool Repository
RustThreadPool code

RustThreadPool  Repository

Process function object on N threads using 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 add post methods
Fig 1. ThreadPool Structure
Fig 2. ThreadPool Output

Concept:

RustThreadPool is a facility for processing a function object concurrently on a specified number of threads, using a thread-safe blocking queue. This is a nice illustration of how to build a data structure that uses threads for concurrent processing. I intend to compare performance of this facility with message passing some time soon.

Design:

There is one struct, ThreadPool<M>, with an associated function and five methods in this design:
  1. new<F>(nt:u8, f:F) -> ThreadPool<M>
    where F: FnOnce(&BlockingQueue<M>) -> () + Send + 'static + Copy
    Create new ThreadPool which is running nt threads, each processing f(). If f is a closure, then input data can be supplied in its capture. Note that f() needs to be tailored for its message type M. Processing is significantly different for string messages versus work items that contain an execution context. Examples of each are given in the test1.rs in examples folder.
  2. wait(&mut self)
    Waits for all threads to complete.
  3. post_message(&mut self, msg: M) where M:Debug + Clone
    Enqueues Messages for processing.
    Function object passed to new needs to accept and process posted Messages.
  4. get_message(&mut self) -> Option<M> where M:Debug + Clone + Default
    Dequeues result message. Option is None if there are no messages to dequeue. Alternate is to simply block on empty, but that may have operational problems.
  5. shut_down(&mut self) -> u8
    Signals threads to exit when queue is empty. Returns size of input queue.
    Sets AtomicBool.
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