4 个版本 (1 个稳定版)
1.0.0 | 2021 年 10 月 1 日 |
---|---|
0.1.2 | 2021 年 10 月 1 日 |
0.1.1 | 2021 年 9 月 30 日 |
0.1.0 | 2021 年 9 月 30 日 |
#1102 在 并发 中
10KB
168 行
pmpmc
优先级多消费者多生产者通道
这是一个为实现了 Ord trait 的元素实现的多生产者多消费者通道的简单实现,它用 rx.recv_greatest() 替换了典型的 rx.recv()。它基本上就是使用典型的 Arc
示例
use pmpmc::pmpmc;
let (tx, rx) = pmpmc();
let tx_new = tx.clone();
let rx_new = rx.clone();
let _ = std::thread::spawn(move || {
tx_new.send(2);
tx_new.send(1);
tx_new.send(3);
})
.join();
assert_eq!(rx_new.recv_greatest(), Some(3));
assert_eq!(rx_new.recv_greatest(), Some(2));
assert_eq!(rx_new.recv_greatest(), Some(1));
assert_eq!(rx_new.recv_greatest(), None);
assert_eq!(rx_new.recv_greatest(), None);
我正在用它来自动调度任务(现在工作者按照优先级顺序从通道中提取任务),但您可以在任何需要从一个或多个线程向列表中推送并排序列表,然后在另一个线程接收之前的情况中使用它。
许可证:MIT