simboli_thread 0.0.2

Thread Pool Management
Documentation
  • Coverage
  • 0%
    0 out of 24 items documented0 out of 0 items with examples
  • Size
  • Source code size: 71.69 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.66 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • araxnoid-code

About

simboli_thread, thread pool management written in rust.

Warning

there is still a memory leak occurring and unstable.

Changelog

changelog.md

Starting

Installation

Run the following Cargo command in your project directory:

cargo add simboli_thread

Or add the following line to your Cargo.toml:

simboli_thread = "0.0.2"

Code

use std::{thread::sleep, time::Duration};
use simboli_thread::{OutputTrait, SimboliThread, TaskTrait, TaskWithDependenciesTrait};
enum MyOutput {
    Number(i32),
    String(String),
    None,
}

impl OutputTrait for MyOutput {}

enum MyTask {
    Exec(fn() -> MyOutput),
    ExecWithDependencies(
        fn(dependencies: &'static Vec<simboli_thread::Waiting<MyOutput>>) -> MyOutput,
    ),
}

impl TaskTrait<MyOutput> for MyTask {
    fn exec(&self) -> MyOutput {
        match self {
            MyTask::Exec(f) => f(),
            _ => MyOutput::None,
        }
    }
}

impl TaskWithDependenciesTrait<MyOutput> for MyTask {
    fn exec(&self, dependencies: &'static Vec<simboli_thread::Waiting<MyOutput>>) -> MyOutput {
        match self {
            MyTask::ExecWithDependencies(f) => f(dependencies),
            _ => MyOutput::None,
        }
    }
}

fn main() {
    let thread_pool = SimboliThread::<MyTask, MyTask, MyOutput, 4, 64>::init();

    let task_1 = thread_pool.spawn_task(MyTask::Exec(|| {
        sleep(Duration::from_millis(100));
        MyOutput::Number(10)
    }));

    let task_2 = thread_pool.spawn_task(MyTask::Exec(|| {
        sleep(Duration::from_millis(100));
        MyOutput::String("done".to_string())
    }));

    // blocking
    let task_1 = task_1.block();
    if let Some(MyOutput::Number(number)) = task_1 {
        println!("task_1 : {}", number)
    }

    let task_2 = task_2.block();
    if let Some(MyOutput::String(str)) = task_2 {
        println!("task_2 : {}", str)
    }

    thread_pool.join();
}