Crates.io | stuck |
lib.rs | stuck |
version | 0.4.0 |
source | src |
created_at | 2022-03-23 06:57:23.229765 |
updated_at | 2024-05-07 05:53:48.027127 |
description | Multi-threading scheduled task facility building on cooperative stackful coroutine |
homepage | https://github.com/kezhuw/stuck |
repository | https://github.com/kezhuw/stuck |
max_upload_size | |
id | 555072 |
size | 295,771 |
Stuck is a multi-threading scheduled task facility building on cooperative stackful coroutine.
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.