本文简要介绍rust语言中 Function std::sync::mpsc::channel 的用法。
用法
pub fn channel<T>() -> (Sender<T>, Receiver<T>)创建一个新的异步通道,返回一半的发送者/接收者。在 Sender 上发送的所有数据都将在 Receiver 上以与发送时相同的顺序可用,并且没有 send 会阻塞调用线程(此通道具有 “infinite buffer”,与 sync_channel 不同,后者达到缓冲区限制后将阻塞)。 recv 将阻塞,直到消息可用,同时至少有一个 Sender 活着(包括克隆)。
Sender 可以多次克隆到同一频道的 send ,但只支持一个 Receiver 。
如果在尝试使用 Sender 连接 send 时 Receiver 断开连接,则 send 方法将返回 SendError 。同样,如果在尝试 recv 时 Sender 断开连接,则 recv 方法将返回 RecvError 。
例子
use std::sync::mpsc::channel;
use std::thread;
let (sender, receiver) = channel();
// Spawn off an expensive computation
thread::spawn(move|| {
sender.send(expensive_computation()).unwrap();
});
// Do some useful work for awhile
// Let's see what that answer was
println!("{:?}", receiver.recv().unwrap());相关用法
- Rust char.is_control用法及代码示例
- Rust char.is_alphanumeric用法及代码示例
- Rust char.len_utf16用法及代码示例
- Rust char.is_digit用法及代码示例
- Rust char.is_ascii_graphic用法及代码示例
- Rust char.decode_utf16用法及代码示例
- Rust char.is_uppercase用法及代码示例
- Rust char.to_ascii_lowercase用法及代码示例
- Rust char.is_ascii_uppercase用法及代码示例
- Rust char.escape_unicode用法及代码示例
- Rust char.is_alphabetic用法及代码示例
- Rust char.is_ascii_control用法及代码示例
- Rust char.from_u32_unchecked用法及代码示例
- Rust char.is_ascii_alphabetic用法及代码示例
- Rust char.eq_ignore_ascii_case用法及代码示例
- Rust char.is_ascii用法及代码示例
- Rust char.make_ascii_lowercase用法及代码示例
- Rust char.is_whitespace用法及代码示例
- Rust char.to_lowercase用法及代码示例
- Rust char.is_ascii_punctuation用法及代码示例
- Rust char.to_digit用法及代码示例
- Rust char.from_digit用法及代码示例
- Rust char.is_lowercase用法及代码示例
- Rust char.encode_utf16用法及代码示例
- Rust char.len_utf8用法及代码示例
注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 Function std::sync::mpsc::channel。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
