Skip to content

Rust threadpool that accepts number of threads and function object in constructor. Uses RustBlockingQueue

Notifications You must be signed in to change notification settings

JimFawcett/RustThreadPool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RustThreadPool

Documentation

Concept:

RustThreadPool is a facility for processing a stream of messages with a function object concurrently on a specified number of threads, using a thread-safe blocking queue. Rust threadpool accepts number of threads and function object in constructor.

Function object uses Messages posted to RustBlockingQueue.

Design:

There is one struct, ThreadPool, with theww methods in this design:

#[derive(Debug)]
pub struct ThreadPool<M> 
{
    sbq: Arc<BlockingQueue<M>>,
    thrd: Vec<Option<JoinHandle<()>>>
    /* see note below about Option */
}
/*-----------------------------------------------------
  construct threadpool, starting nt threads,
  provide threadpool processing as f:F in new 
*/
pub fn new<F>(nt:u8, f:F) -> ThreadPool<M> 
where F: FnOnce(&BlockingQueue<M>) -> () + Send + 'static + Copy

/*-- wait for threads to finish --*/
pub fn wait(&mut self)

/*-- post to ThreadPool queue --*/
pub fn post_message(&mut self, _msg:M) 
where M:Debug + Clone 

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 can be shared between threads because it only has two fields and those are share-able. One is a Mutex<VecDeque>, and the other is a Condvar, e.g., a condition variable.

Operation:

Operation is illustrated by the file test1.rs in /examples.

Build:

Download and, in a command prompt, use one of the following:

  • cargo build
  • cargo test
  • cargo run --example test1.

Status:

ThreadPool has been used in several projects in this repository. You may wish to look at RustCommExperiments

About

Rust threadpool that accepts number of threads and function object in constructor. Uses RustBlockingQueue

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages