#worker-thread #run-time #multi-threading #boilerplate #tasks #control-flow

employees

A small runtime that hides all the boilerplate when using threads

6 releases

0.0.6 Apr 8, 2024
0.0.5 Apr 5, 2024
0.0.4 Dec 15, 2023
0.0.3 Nov 21, 2023

#266 in Concurrency

Download history 1/week @ 2024-02-17 8/week @ 2024-02-24 62/week @ 2024-03-30 121/week @ 2024-04-06

183 downloads per month

MIT license

66KB
879 lines

employees

A small and lightweight crate to hide most of the burden of setting up threads.

Philosophy

This crate sees threads as unique entities called workers which live as long as the program lives.

One may notice many similarities with async tasks but there is one major difference. While tasks are designed to be short and non-blocking pieces of concurrent code running in async runtimes, workers on the other hand are the complete opposite:

  • they run on their own OS thread.
  • they are designed to run for a very long time (mostly for the lifetime of the program).
  • they can block on operations as long as they (mostly) want without impacting other workers.

Some similarities exist between a full-blown Entity Component System and this crate. In some regards, this crate can be viewed as an ECS without the C part.

Usage

Here's a small example that spawns a worker that prints "Hello, World!" every 100ms for 1 second.

struct WorkerThatPrints;
impl Worker for WorkerThatPrints {
    fn on_update(&mut self) -> ControlFlow {
        println!("Hello, World!");
        std::thread::sleep(Duration::from_millis(100));
        ControlFlow::Continue
    }
}

let mut runtime = Runtime::new();

runtime.launch(WorkerThatPrints);
std::thread::sleep(Duration::from_secs(1));

See the full documentation for more in depth details.

License

Licensed under the terms of MIT license. See LICENSE for details.

Dependencies

~0.2–9MB
~48K SLoC