Skip to main content

Crate poolio

Crate poolio 

Source
Expand description

poolio is a thread pool implementation using only channels for concurrency.

§Design

A poolio thread pool is essentially made up of a ‘supervisor’ thread and a specified number of ‘worker’ threads. A worker’s only purpose is executing jobs (in the form of closures), while the supervisor is responsible for everything else - most importantly, assigning jobs to workers that it receives from outside the pool via the public API. To this end, the thread pool is set up so that the supervisor can communicate with each worker separately and concurrently. This ensures that each worker remains equally busy. A single supervisor-worker communication cycle is roughly as follows:

  1. The worker tells the supervisor its current status.
  2. The supervisor decides what to tell the worker to do based on the current order-message from outside the pool and the worker’s status.
  3. The supervisor tells the worker what to do.
  4. The worker attempts to perform the task assigned by the supervisor.
  5. The worker tells the supervisor its current status.

The following graphic illustrates the aforementioned communication model between a supervisor thread S and a worker thread W:

   W
   _
   .
   .
   send-status
   .   O
   .     O
   .       O                 send-message
   .         O                   O
   .           O               O
   recv         recv         O
  * .  O       O  . .      O
 .   .   O   O   .   .   O
.     e    O    m     recv . . | S
 .   .   O   O   .   *
  . .  O       O  . .
   send-status  send-message

X | . . * : arrow starting at | and ending at * representing the control-flow of thread X
O O O O O : channel
e : execute job
m : manage workers

§Usage

To use a poolio ThreadPool, you simply set one up using the ThreadPool::new method and task the pool to run jobs using the ThreadPool::execute method.

§Examples

Setting up a pool to make a server multi-threaded:

fn handle(req: usize) {
    println!("Handled!")
}

let server_requests = [1, 2, 3, 4, 5, 6, 7, 8, 9];

let pool = poolio::ThreadPool::new(3, poolio::PanicSwitch::Kill).unwrap();

for req in server_requests {
    pool.execute(move || {
        handle(req);
    });
}

Structs§

ThreadPool
Abstracts thread pools.

Enums§

PanicSwitch
Configuration for how the ThreadPool handles panics in jobs.