```rust use aqueue::AQueue; static mut VALUE:i32=0;
async fn main()->Result<(),Box
assert_eq!(v,-1455759936);
}
```
```rust
use aqueue::{AResult,AQueue}; use std::sync::Arc; use std::cell::{RefCell}; use std::error::Error; use std::time::Instant;
struct Foo{ count:u64, i:i128 }
impl Foo{ pub fn add(&mut self,x:i32)->i128{ self.count+=1; self.i+=x as i128; self.i }
pub fn get(&self)->i128{
self.i
}
pub fn get_count(&self)->u64{
self.count
}
}
struct Store
impl
struct FooRunner {
inner:Arc
impl FooRunner {
pub fn new()-> FooRunner {
FooRunner {
inner:Arc::new(Store::new(Foo{ count:0, i:0})),
queue:AQueue::new()
}
}
pub async fn add(&self,x:i32)->AResult
pub async fn get(&self)->AResult<i128>{
self.queue.run(async move |inner| {
Ok(inner.0.borrow().get())
},self.inner.clone()).await
}
pub async fn get_count(&self)->AResult<u64>{
self.queue.run(async move |inner| {
Ok(inner.0.borrow().get_count())
},self.inner.clone()).await
}
}
async fn main()->Result<(),Box
let start = Instant::now();
for i in 0..2000000 {
if let Err(er) = tf.add(i).await {
println!("{}", er);
};
}
println!("test a count:{} value:{} time:{} qps:{}",
tf.get_count().await?,
tf.get().await?,
start.elapsed().as_secs_f32(),
tf.get_count().await? / start.elapsed().as_millis() as u64 * 1000);
}
{
//Multithreading test
let tf = Arc::new(FooRunner::new());
let start = Instant::now();
let a_tf = tf.clone();
let a = tokio::spawn(async move {
for i in 0..1000000 {
if let Err(er) = a_tf.add(i).await {
println!("{}", er);
};
}
});
let b_tf = tf.clone();
let b = tokio::spawn(async move {
for i in 1000000..2000000 {
if let Err(er) = b_tf.add(i).await {
println!("{}", er);
};
}
});
let c_tf = tf.clone();
let c = tokio::spawn(async move {
for i in 2000000..3000000 {
if let Err(er) = c_tf.add(i).await {
println!("{}", er);
};
}
});
c.await?;
a.await?;
b.await?;
println!("test b count:{} value:{} time:{} qps:{}",
tf.get_count().await?,
tf.get().await?,
start.elapsed().as_secs_f32(),
tf.get_count().await? / start.elapsed().as_millis() as u64 * 1000);
}
Ok(())
} ```
```rust
use aqueue::{Actor,AResult}; use std::sync::Arc; use std::error::Error; use std::time::Instant; use asynctrait::asynctrait;
struct Foo{ count:u64, i:i128 }
impl Foo{
pub fn add(&mut self,x:i32)->i128{
self.count+=1;
self.i+=x as i128;
self.i
}
fn reset(&mut self){
self.count=0;
self.i=0;
}
pub fn get(&self)->i128{
self.i
}
pub fn get_count(&self)->u64{
self.count
}
}
pub trait FooRunner{
async fn add(&self,x:i32)->Result
impl FooRunner for Actor
async fn add(&self,x:i32)->Result
async fn reset(&self)->Result<(),Box
async fn get(&self)->Result
async fn getcount(&self)->Result
async fn main()->Result<(),Box
let start = Instant::now();
for i in 0..2000000 {
if let Err(er) = tf.add(i).await {
println!("{}", er);
};
}
println!("test a count:{} value:{} time:{} qps:{}",
tf.get_count().await?,
tf.get().await?,
start.elapsed().as_secs_f32(),
tf.get_count().await? / start.elapsed().as_millis() as u64 * 1000);
}
{
//Multithreading test
let tf = Arc::new(Actor::new(Foo::default()));
let start = Instant::now();
let a_tf = tf.clone();
let a = tokio::spawn(async move {
for i in 0..1000000 {
if let Err(er) = a_tf.add(i).await {
println!("{}", er);
};
}
});
let b_tf = tf.clone();
let b = tokio::spawn(async move {
for i in 1000000..2000000 {
if let Err(er) = b_tf.add(i).await {
println!("{}", er);
};
}
});
let c_tf = tf.clone();
let c = tokio::spawn(async move {
for i in 2000000..3000000 {
if let Err(er) = c_tf.add(i).await {
println!("{}", er);
};
}
});
c.await?;
a.await?;
b.await?;
println!("test b count:{} value:{} time:{} qps:{}",
tf.get_count().await?,
tf.get().await?,
start.elapsed().as_secs_f32(),
tf.get_count().await? / start.elapsed().as_millis() as u64 * 1000);
}
Ok(())
} ```