简体   繁体   English

std :: sync :: mpsc :: channel始终以相同顺序

[英]std::sync::mpsc::channel always in the same order

No matter how many times I run the program, it always shows the numbers in the same order: 无论我运行该程序多少次,它始终以相同的顺序显示数字:

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

fn main() {
    let (tx, rx) = channel();
    for i in 0 ..10 {
        let tx = tx.clone();
        thread::spawn(move || {
            tx.send(i).unwrap();
        }); 
    }

    for _ in 0..10 {
        println!("{}", rx.recv().unwrap());
    }
}

Code on the playground . 操场上编码。 The output is: 输出为:

6
7
8
5
9
4
3
2
1
0

If I rebuild the project, the sequence will change. 如果我重建项目,顺序将改变。 Is the sequence decided at compile time? 顺序是在编译时决定的吗?

What order would you expect them to be in? 您希望他们以什么顺序? For what it's worth, on my machine I ran the same binary twice and got slightly different results. 就其价值而言,我在我的计算机上运行了两次相同的二进制文件,结果略有不同。

Ultimately, this comes down to how your operating system decides to schedule threads. 最终,这取决于您的操作系统决定调度线程的方式。 You create 10 new threads and then ask the OS to run each of them when convenient. 您创建10个新线程,然后在方便时要求操作系统运行它们。 A hypothetical thread scheduler might look like this: 假设的线程调度程序可能如下所示:

for thread in threads {
    if thread.runnable() {
        thread.run_for_a_time_slice();
    }
}

Where threads stores the threads in the order they were created. threads专卖店在创建它们的顺序线程。 It's unlikely that any OS would be this naïve, but it shows the idea. 任何操作系统都不会这么幼稚,但这表明了这个想法。

In your case, every thread is ready to run immediately, and is very short so it can run all the way to completion before the time is up. 在您的情况下,每个线程都准备好立即运行,并且线程很短,因此可以在时间到了之前一直运行到完成。

Additionally, there might be some fairness being applied to the lock that guards the channel. 此外,可能会对保护通道的锁施加一些公平性。 Perhaps it always lets the first of multiple competing threads submit a value. 也许它总是让多个竞争线程中的第一个提交值。 Unfortunately, the implementation of channels is reasonably complex, so I can't immediately say if that's the case or not. 不幸的是,渠道的实施相当复杂,所以我不能立即说是否如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作一个简单的 futures::sync::mpsc::channel 示例? - How to make a simple futures::sync::mpsc::channel example work? Rust lazy_static 和 tokio::sync::mpsc::channel in tokio::select - Rust lazy_static and tokio::sync::mpsc::channel in tokio::select 为什么tokio::sync::Notify示例代码中的channel是mpsc? - Why the channel in the example code of tokio::sync::Notify is a mpsc? 如何传递 std::sync::mpsc::Sender<t> 经线处理程序?</t> - How to pass a std::sync::mpsc::Sender<T> to a handler in warp? 没有为 `std::sync::mpsc::Sender 实现特征`std::marker::Sync`<i32></i32> - the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender<i32> 使用相同的 Rust 通道(mpsc)发送不同类型 - Sending different types using same Rust channel (mpsc) 如何使用futures :: sync :: mpsc :: channel实现阻塞队列机制? - How can I implement a blocking queue mechanism with futures::sync::mpsc::channel? 将数据从流转发到Tokio mpsc通道 - Forwarding data to a Tokio mpsc channel from a stream 为什么`futures :: channel :: mpsc`只能通知一个发件人? - Why `futures::channel::mpsc` can just notify one sender? 如果没有数据等待,则从mpsc :: channel获取数据而不锁定它 - Grabbing data from mpsc::channel without having it lock if there is no data waiting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM