1

Sender in std::sync::mpsc - Rust

 1 year ago
source link: https://doc.rust-lang.org/stable/std/sync/mpsc/struct.Sender.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The sending-half of Rust’s asynchronous channel type. This half can only be owned by one thread, but it can be cloned to send to other threads.

Messages can be sent through this channel with send.

Note: all senders (the original and the clones) need to be dropped for the receiver to stop blocking to receive messages with Receiver::recv.

Examples

use std::sync::mpsc::channel;
use std::thread;

let (sender, receiver) = channel();
let sender2 = sender.clone();

// First thread owns sender
thread::spawn(move || {
    sender.send(1).unwrap();
});

// Second thread owns sender2
thread::spawn(move || {
    sender2.send(2).unwrap();
});

let msg = receiver.recv().unwrap();
let msg2 = receiver.recv().unwrap();

assert_eq!(3, msg + msg2);
Run

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK