本文簡要介紹rust語言中 std::sync::mpsc::SyncSender.send
的用法。
用法
pub fn send(&self, t: T) -> Result<(), SendError<T>>
在此同步通道上發送一個值。
此函數將阻塞,直到內部緩衝區中的空間變得可用或接收者可以將消息傳遞給。
請注意,成功發送不會不是保證如果該通道上有緩衝區,接收方將看到數據。項目可以在內部緩衝區中排隊,以便接收者稍後接收。然而,如果緩衝區大小為 0,則該通道將成為匯聚通道,並且如果該函數返回成功,則可以保證接收者確實已收到數據。
此函數永遠不會Panics,但如果 Receiver
已斷開連接並且不再能夠接收信息,它可能會返回 Err
。
例子
use std::sync::mpsc::sync_channel;
use std::thread;
// Create a rendezvous sync_channel with buffer size 0
let (sync_sender, receiver) = sync_channel(0);
thread::spawn(move || {
println!("sending message...");
sync_sender.send(1).unwrap();
// Thread is now blocked until the message is received
println!("...message received!");
});
let msg = receiver.recv().unwrap();
assert_eq!(1, msg);
相關用法
- Rust SyncSender.try_send用法及代碼示例
- Rust SyncSender用法及代碼示例
- Rust SyncOnceCell用法及代碼示例
- Rust SyncLazy用法及代碼示例
- Rust SyncOnceCell.get_or_try_init用法及代碼示例
- Rust SyncOnceCell.get_or_init用法及代碼示例
- Rust SyncOnceCell.set用法及代碼示例
- Rust SyncOnceCell.take用法及代碼示例
- Rust SyncOnceCell.into_inner用法及代碼示例
- Rust SyncLazy.force用法及代碼示例
- Rust SystemTime.elapsed用法及代碼示例
- Rust SystemTimeError.duration用法及代碼示例
- Rust SystemTimeError用法及代碼示例
- Rust SymmetricDifference用法及代碼示例
- Rust System用法及代碼示例
- Rust SystemTime.now用法及代碼示例
- Rust SystemTime用法及代碼示例
- Rust SystemTime.duration_since用法及代碼示例
- Rust String.try_reserve用法及代碼示例
- Rust Saturating.reverse_bits用法及代碼示例
- Rust Seek.stream_len用法及代碼示例
- Rust SplitNMut用法及代碼示例
- Rust SocketAddrV6.ip用法及代碼示例
- Rust Shl.shl用法及代碼示例
- Rust SubAssign.sub_assign用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::sync::mpsc::SyncSender.send。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。