#stackful-coroutine #coroutine #task #thread

stuck

Multi-threading scheduled task facility building on cooperative stackful coroutine

12 releases

0.4.0 May 7, 2024
0.3.3 Apr 13, 2024
0.3.1 Apr 23, 2022
0.2.0 Apr 20, 2022
0.1.2 Mar 26, 2022

#355 in Concurrency

Download history 55/week @ 2024-07-26 6/week @ 2024-08-02 2/week @ 2024-09-20 41/week @ 2024-09-27 5/week @ 2024-10-04 1/week @ 2024-10-11

716 downloads per month

MIT license

260KB
7K SLoC

stuck

crates.io github-ci codecov docs.rs mit-license

Stuck is a multi-threading scheduled task facility building on cooperative stackful coroutine.

Examples

use std::time::Duration;

use stuck::channel::parallel;
use stuck::channel::prelude::*;
use stuck::{select, task, time};

#[stuck::main]
fn main() {
    let (mut request_sender, request_receiver) = parallel::bounded(1);
    let (mut response_sender, mut response_receiver) = parallel::bounded(1);

    task::spawn(move || {
        for value in request_receiver.into_iter() {
            time::sleep(Duration::from_secs(1));
            response_sender.send(value - 1).unwrap();
        }
    });

    let mut tasks = vec![6, 6, 6, 6];

    let mut sum = 0;
    loop {
        select! {
            r = <-response_receiver => if let Some(n) = r {
                sum += n;
            },
            _ = request_sender<-tasks.pop().unwrap(), if !tasks.is_empty()  => if tasks.is_empty() {
                request_sender.close();
            },
            complete => break,
        }
    }
    println!("sum: {}", sum);
    assert_eq!(sum, 20);
}

See tests for more examples.

LICENSE

MIT

Inspiration

  • stp: The C++ counterpart that this library derives from.
  • skynet: A lightweight online game framework

Dependencies

~2–12MB
~86K SLoC