ringbuf-可直接访问内部数据的无锁SPSC FIFO环形缓冲区-Rust开发

  • d9_234670
    了解作者
  • 26.9KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 05:30
    上传日期
ringbuf无锁单生产者单消费者(SPSC)FIFO环形缓冲区,可直接访问内部数据。 概述RingBuffer是最初的结构ringbuf无锁单生产者单消费者(SPSC)FIFO环形缓冲区,可直接访问内部数据。 概述RingBuffer是代表环形缓冲区本身的初始结构。 环形缓冲区可以分为生产者和消费者两对。 生产者和消费者用于相应地将元素添加到环形缓冲区或从环形缓冲区中删除元素。 它们可以在线程之间安全地传输。 生产者和使用者的操作是无锁的-它们成功成功或立即失败而没有
ringbuf-master.zip
  • ringbuf-master
  • .travis.yml
    1.1KB
  • LICENSE-MIT
    1KB
  • LICENSE-APACHE
    10.6KB
  • appveyor.yml
    1.4KB
  • src
  • ring_buffer.rs
    5.3KB
  • tests
  • drop.rs
    5.5KB
  • mod.rs
    125B
  • read_write.rs
    4.3KB
  • single.rs
    5KB
  • access.rs
    8.2KB
  • multiple.rs
    3.4KB
  • message.rs
    6.2KB
  • producer.rs
    9.6KB
  • consumer.rs
    13.7KB
  • benchmark.rs
    1.2KB
  • lib.rs
    1.9KB
  • Cargo.toml
    689B
  • examples
  • simple.rs
    423B
  • message.rs
    1.7KB
  • .gitignore
    30B
  • README.md
    4.4KB
内容介绍
# ringbuf [![Crates.io][crates_badge]][crates] [![Docs.rs][docs_badge]][docs] [![Travis CI][travis_badge]][travis] [![Appveyor][appveyor_badge]][appveyor] [![Codecov.io][codecov_badge]][codecov] [![License][license_badge]][license] [crates_badge]: https://img.shields.io/crates/v/ringbuf.svg [docs_badge]: https://docs.rs/ringbuf/badge.svg [travis_badge]: https://api.travis-ci.org/agerasev/ringbuf.svg [appveyor_badge]: https://ci.appveyor.com/api/projects/status/github/agerasev/ringbuf?branch=master&svg=true [codecov_badge]: https://codecov.io/gh/agerasev/ringbuf/graphs/badge.svg [license_badge]: https://img.shields.io/crates/l/ringbuf.svg [crates]: https://crates.io/crates/ringbuf [docs]: https://docs.rs/ringbuf [travis]: https://travis-ci.org/agerasev/ringbuf [appveyor]: https://ci.appveyor.com/project/agerasev/ringbuf [codecov]: https://codecov.io/gh/agerasev/ringbuf [license]: #license Lock-free single-producer single-consumer (SPSC) FIFO ring buffer with direct access to inner data. # Overview `RingBuffer` is the initial structure representing ring buffer itself. Ring buffer can be splitted into pair of `Producer` and `Consumer`. `Producer` and `Consumer` are used to append/remove elements to/from the ring buffer accordingly. They can be safely transfered between threads. Operations with `Producer` and `Consumer` are lock-free - they're succeded or failed immediately without blocking or waiting. Elements can be effectively appended/removed one by one or many at once. Also data could be loaded/stored directly into/from [`Read`]/[`Write`] instances. And finally, there are `unsafe` methods allowing thread-safe direct access in place to the inner memory being appended/removed. [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html When building with nightly toolchain it is possible to run benchmarks via `cargo bench --features benchmark`. Also the crate could be used with `no_std` (but `alloc` is still required). # Examples ## Simple example ```rust use ringbuf::RingBuffer; let rb = RingBuffer::<i32>::new(2); let (mut prod, mut cons) = rb.split(); prod.push(0).unwrap(); prod.push(1).unwrap(); assert_eq!(prod.push(2), Err(2)); assert_eq!(cons.pop().unwrap(), 0); prod.push(2).unwrap(); assert_eq!(cons.pop().unwrap(), 1); assert_eq!(cons.pop().unwrap(), 2); assert_eq!(cons.pop(), None); ``` ## Message transfer This is more complicated example of transfering text message between threads. ```rust use std::io::Read; use std::thread; use std::time::Duration; use ringbuf::RingBuffer; let buf = RingBuffer::<u8>::new(10); let (mut prod, mut cons) = buf.split(); let smsg = "The quick brown fox jumps over the lazy dog"; let pjh = thread::spawn(move || { println!("-> sending message: '{}'", smsg); let zero = [0]; let mut bytes = smsg.as_bytes().chain(&zero[..]); loop { if prod.is_full() { println!("-> buffer is full, waiting"); thread::sleep(Duration::from_millis(1)); } else { let n = prod.read_from(&mut bytes, None).unwrap(); if n == 0 { break; } println!("-> {} bytes sent", n); } } println!("-> message sent"); }); let cjh = thread::spawn(move || { println!("<- receiving message"); let mut bytes = Vec::<u8>::new(); loop { if cons.is_empty() { if bytes.ends_with(&[0]) { break; } else { println!("<- buffer is empty, waiting"); thread::sleep(Duration::from_millis(1)); } } else { let n = cons.write_into(&mut bytes, None).unwrap(); println!("<- {} bytes received", n); } } assert_eq!(bytes.pop().unwrap(), 0); let msg = String::from_utf8(bytes).unwrap(); println!("<- message received: '{}'", msg); msg }); pjh.join().unwrap(); let rmsg = cjh.join().unwrap(); assert_eq!(smsg, rmsg); ``` ## License Licensed under either of * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option. ### Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
评论
    相关推荐
    • Rust
      Rust
    • rust
      安装rustup工具链: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    • rust-cityhash
      锈城 CityHash是一系列非加密哈希函数,旨在用于字符串的快速哈希。 Cityhash使用Austin Appleby的MurmurHash的想法。 有关CityHash的更多信息,请参见 这是Google的CityHash版本1.1.1移植到纯Rust的端口
    • Rust
      Rust
    • rust
      代码空间Rust Starter 该项目是供开发人员在Codespaces中使用的通用入门,它包括基本的系统工具和扩展。 包含什么 这是一个基本环境,应该准备好对其进行扩展,以构建Rust的日常开发环境。 它带有以下软件选择: ...
    • rust-zmq:Rust zeromq绑定
      Rust ZeroMQ绑定。 关于 zmq板条箱为项目中的libzmq库提供了绑定。 zmq公开的API应该是安全的(通常在Rust的意义上),但是它紧跟C API,因此不是很惯用。 同样,在“草稿”状态下对libzmq API的支持也被视为该板条...
    • rust-enforce
      Rust 中的流体断言 例子 使用来测试语法 describe ! vec { describe ! push { before_each { let mut v = vec! [1u, 2 , 3 ]; } it "should increase the length by 1" { let old = v. len (); v. push (4u);...
    • rust-learning
      我的Rust学习库 基于Traversy Media的Brad的这段视频 任务列表 在Rust中打印 Rust中的变量 Rust的类型 锈中的弦 Rust中的元组 Rust中的数组 [] Rust中的载体
    • Rust的LibYAML绑定-Rust开发
      用于Rust依赖项LibYAML 0.1.4或更高版本的libyaml-rust LibYAML绑定稳定Rust(2015/2018版)用法从内存extern crate yaml解析; 用于Rust依赖项LibYAML 0.1.4或更高版本的libyaml-rust LibYAML绑定稳定Rust(2015/...
    • rust
      rust