Crates.io | threadder |
lib.rs | threadder |
version | 0.1.0 |
source | src |
created_at | 2021-04-23 06:27:49.713688 |
updated_at | 2021-04-23 06:27:49.713688 |
description | A multi-threading implementation that does not rely on Futures. |
homepage | |
repository | https://github.com/T-O-R-U-S/threadder |
max_upload_size | |
id | 388466 |
size | 8,941 |
A simple multithreading implementation. Examples:
use std::{
thread::sleep,
time::Duration
};
use threadder::ThreadPool;
fn main() {
// Warning: Threadpools MUST be declared as mutable!
// If the threadpool is not mutable, it won't be able to switch threads.
// Create a 4-thread threadpool
let mut my_threadpool = ThreadPool::new(4);
for _ in 0..4 {
// Clojures can (optionally) be boxed.
my_threadpool.send(
|| {
println!("Goodnight, world!");
sleep(Duration::from_secs(3));
println!("Goodmorning, world!");
}
);
}
// MUST be ran at the end of the program. Ensures all threads exit PROPERLY. Without this,
// all threads will shut down as soon as the 'main' thread shuts down -- which isn't desirable
// when your thread finishes 3 seconds AFTER main does.
my_threadpool.stop();
// This drops the transmitter which breaks the listener
// loop and joins the thread, allowing it to exit properly without landing the program in an endless loop
}