Crate kirk [−] [src]
Pools of generic crews that perform generic jobs, statically or dynamically.
Pools
With fewest restrictions, a pool could run any task.
use kirk::{Pool, Task, Deque}; use kirk::crew::deque::Options; let options = Options::default(); let mut pool = Pool::<Deque<Task>>::new(options); pool.push(|| { println!("Hello!") }); pool.push(|| { println!("World!") });
Crews
Crews abstract the method of sending jobs to workers. Another implementation
is Channel
.
use kirk::{Pool, Task, Channel}; use kirk::crew::channel::Options; let options = Options::default(); let mut pool = Pool::<Channel<Task>>::new(options); pool.push(|| { println!("Hello!") }); pool.push(|| { println!("World!") });
The goal is to support various methods for different workloads, while making it easy to switch from one to another.
Jobs and Tasks
The core abstraction is Job
, which has a single, consuming method,
perform
. Defining a data structure that implements Job
allows worker
threads to statically dispatch the call to perform
.
use kirk::{Pool, Job, Deque}; use kirk::crew::deque::Options; enum Msg { Hello, World }; impl Job for Msg { fn perform(self) { match self { Msg::Hello => println!("Hello!"), Msg::World => println!("World!"), } } } let options = Options::default(); let mut pool = Pool::<Deque<Msg>>::new(options); pool.push(Msg::Hello); pool.push(Msg::World);
As shown before, however, there's a predefined type Task
, a boxed closure.
This allows arbitary jobs to be pushed to a pool, but with added costs both
to construct the box and to call each dynamically.
Scoping
Using crossbeam::scope
, jobs can safely access data on the stack of the
original caller, like so:
extern crate crossbeam; extern crate kirk; let mut items = [0usize; 8]; crossbeam::scope(|scope| { let options = kirk::crew::deque::Options::default(); let mut pool = kirk::Pool::<kirk::Deque<kirk::Task>>::scoped(&scope, options); for (i, e) in items.iter_mut().enumerate() { pool.push(move || *e = i) } });
Reexports
pub use crew::{Crew, Parameters, Worker}; |
pub use crew::channel::Channel; |
pub use crew::deque::Deque; |
Modules
crew |
Traits that abstract how to send jobs to workers. |
Structs
Pool |
A crew of workers that perform jobs on separate threads. |
Task |
A boxed closure that can be performed as a job. |
Traits
Job |
An item of work to be executed in a thread pool. |