tokryon bak

This commit is contained in:
Christoph J. Scherr 2024-02-01 10:39:52 +01:00
parent 5a0f83695c
commit c59941ad7d
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
2 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,11 @@
[package]
name = "tokryon"
version = "0.0.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
futures = "0.3.30"
rayon = "1.8.1"
tokio = { version = "1.35.1", features = ["rt", "macros", "sync", "time", "rt-multi-thread"] }

View File

@ -0,0 +1,56 @@
use std::sync::atomic::{AtomicU32, Ordering};
use rayon::prelude::*;
use tokio::{
self,
time::{interval, Interval},
};
static THE_NUMBER: AtomicU32 = AtomicU32::new(0);
#[inline]
fn incr() {
// just add it
THE_NUMBER.store(THE_NUMBER.load(Ordering::Relaxed) + 1, Ordering::Relaxed)
}
#[inline]
fn get() -> u32 {
THE_NUMBER.load(Ordering::Relaxed)
}
#[inline]
async fn more() {
rayon::spawn(|| incr())
}
#[tokio::main]
async fn main() {
// Lets say that we want to add numbers FAST
println!("The number: {THE_NUMBER:?}");
incr();
assert_eq!(get(), 1);
println!("The number: {THE_NUMBER:?}");
println!("starting the threads");
let mut interval = interval(tokio::time::Duration::from_millis(100));
loop {
match future::select(do_work(), interval.tick()).await {
Either::Left((result, _)) => {
// Our worker completed successfully!
result?;
// Do any additional processing here after successful completion
},
Either::Right(_) => {
// The interval has fired - we don't have to wait for the worker anymore
println!("Interval triggered!");
}
};
tokio::select! {
_ = interval.tick() => {
println!("The number: {THE_NUMBER:?}");
}
_ = more() => ()
};
}
}