1 unstable release
0.1.0 | Feb 20, 2021 |
---|
#1141 in Concurrency
17,076 downloads per month
59KB
748 lines
Async lock-free Ringbuffer
FixedRingBuffer is an asynchronous SPSC fixed-capacity look-free ring buffer, which can be used to transfer data between two threads or between two asynchronous tasks. The overall feature is as follows:
- lock-free
- spsc
- Use a fixed-capacity buffer, and support buffer internal memory recycle
- Implement AsyncRead and AsyncBufRead trait for reader
- Implements AsyncWrite trait for writer
Quick Start
use std::sync::Arc;
use bytes::BufMut;
use async_std::task;
use std::{thread};
use futures::io::{AsyncWriteExt, AsyncReadExt};
use futures_lite::future;
use crate::async_ring_buffer::{RingBufferReader, RingBufferWriter, RingBuffer};
let ring_buffer = Arc::new(RingBuffer::new(1024));
let mut reader = RingBufferReader::new(ring_buffer.clone());
let mut writer = RingBufferWriter::new(ring_buffer.clone());
let t1 = thread::spawn(move ||{
let handle = task::spawn(async move {
let mut length = 0 as usize;
let mut contents: Vec<u8> = Vec::with_capacity(16096);
contents.resize(16096, 0);
loop {
match reader.read(&mut contents).await {
Ok(size) => {
if size > 0 {
length += size;
} else {
break;
}
},
Err(e) => {
panic!("read err = {}", e);
},
}
}
println!("length = {}", length);
});
task::block_on(handle);
});
let t2 = thread::spawn(move ||{
let handle = task::spawn(async move {
let mut length = 0 as usize;
let mut contents: Vec<u8> = Vec::new();
contents.put("example-data-data".as_bytes());
for i in 0..102400 as usize {
match writer.write_all(&mut contents).await {
Ok(()) => {
length += contents.len();
},
Err(e) => {
panic!("write err = {}", e);
},
}
}
println!("length = {}", length);
});
task::block_on(handle);
});
t1.join();
t2.join();
Dependencies
~6–15MB
~216K SLoC