#channel #thread #mpsc #fifo #producer-consumer

no-std uchan

多生产者单消费者通道用于消息传递

6个版本

0.1.4 2022年11月5日
0.1.3 2022年11月5日
0.0.1 2022年5月25日

#981 in 并发

MIT 许可协议

49KB
866

μchan

小巧、可扩展、无界、mpsc通道。

Cargo Documentation License

这是一个(几乎)完全可替换 std::sync::mpsc 的库,重点关注无锁和可扩展性,适用于生产者和消费者。它还支持在 #![no_std] 环境中使用,其中调用者提供一个用于阻塞和解除阻塞线程的trait,队列实现其他所有功能。

使用方法

[dependencies]
uchan = "0.1.4"

基准测试

cd benchmark
cargo run --release

要向基准测试添加自定义通道,请参阅 benchmark/src/queues.rs

许可协议

uchan 使用 MIT (http://opensource.org/licenses/MIT) 许可协议


lib.rs:

多生产者,单消费者FIFO队列通信原语。

此模块通过通道提供基于消息的通信,具体定义在两种类型之间

Sender 用于向 Receiver 发送数据。发送者可复制(多生产者),因此许多线程可以同时向一个接收者发送数据(单消费者)。

目前有一个版本可用:异步、无限缓冲的通道。 channel 函数将返回一个 (Sender, Receiver) 元组,其中所有发送都将 异步(它们永远不会阻塞)。从概念上讲,通道具有无限缓冲区。

no_std 使用方法

由于线程阻塞功能已通用化,通道可以在 no_std 设置中使用。使用 Event 特性实现线程挂起,并使用 raw_channel 创建自定义的 RawSenderReceiver。默认的 SenderReceiver 使用 StdEvent,该事件实现了使用 std::thread::park 的线程挂起。

断开连接

通道上的发送和接收操作都将返回一个 Result,指示操作是否成功。操作失败通常表明通道的另一部分在对应的线程中被丢弃而“挂起”。

一旦通道的一半被回收,大多数操作将无法继续进行,因此将返回 [Err]。许多应用程序将继续从该模块返回的结果中 unwrap,如果在意外死亡的情况下,将导致线程之间的失败传播。

示例

简单使用

use std::thread;
use uchan::channel;

// Create a simple streaming channel
let (tx, rx) = channel();
thread::spawn(move|| {
    tx.send(10).unwrap();
});
assert_eq!(rx.recv().unwrap(), 10);

共享使用

use std::thread;
use uchan::channel;

// Create a shared channel that can be sent along from many threads
// where tx is the sending half (tx for transmission), and rx is the receiving
// half (rx for receiving).
let (tx, rx) = channel();
for i in 0..10 {
    let tx = tx.clone();
    thread::spawn(move|| {
        tx.send(i).unwrap();
    });
}

for _ in 0..10 {
    let j = rx.recv().unwrap();
    assert!(0 <= j && j < 10);
}

传播恐慌

use uchan::channel;

// The call to recv() will return an error because the channel has already
// hung up (or been deallocated)
let (tx, rx) = channel::<i32>();
drop(tx);
assert!(rx.recv().is_err());

依赖关系

~69KB