Create one or multi threads

use std::thread;

fn main() {
    start_one_thread();
    start_one_thread_result();
    start_two_threads();

    start_n_threads();
}

pub fn start_one_thread() {
    let handle = thread::spawn(|| {
        println!("Hello from a thread!");
    });

    handle.join().unwrap(); // 等待线程执行完成
}

pub fn start_one_thread_result() {
    let handle = thread::spawn(|| {
        println!("Hell from a thread!");
        200
    });

    match handle.join() {
        Ok(v) => println!("thread result: {}", v),
        Err(e) => println!("error: {:?}", e),
    }
}

pub fn start_two_threads() {
    let handle1 = thread::spawn(|| {
        println!("Hello from a thread1!");
    });

    let handle2 = thread::spawn(|| {
        println!("Hello from a thread2!");
    });

    handle1.join().unwrap();
    handle2.join().unwrap();
}

pub fn start_n_threads() {
    const N: isize = 10;

    let handles = (0..N).map(|i| {
        thread::spawn(move || {
            println!("Hello from a thread{}!", i);
        })
    });

    handles.for_each(|handle|{
        handle.join().unwrap()
    })

    // for handle in handles {
    //     handle.join().unwrap();
    // }
}

(Playground)

Output:

Hello from a thread!
Hell from a thread!
thread result: 200
Hello from a thread2!
Hello from a thread1!
Hello from a thread0!
Hello from a thread1!
Hello from a thread2!
Hello from a thread3!
Hello from a thread4!
Hello from a thread5!
Hello from a thread6!
Hello from a thread7!
Hello from a thread8!
Hello from a thread9!

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.75s
     Running `target/debug/playground`

(A Chinese translation follows below / 中文翻译在下方)

Hi, welcome back! As I’ve already explained in my reply to your last topic

you’re using this forum not quite in the way it’s supposed to be used. The point of the forum is for having discussion or answering questions. Sharing tutorials and discussing them is also a good use-case, (which is what the "tutorials" label you’re using is usually good for).

If you want to collect small code examples, e.g. for your own learning, or as a collection for others, opening a new topic for each one – with little prospect of useful discussion about them – is unfortunately going to be considered a bit too spammy for our forum. I am thus going to unlist some of your topics, and I have to ask you to find a different way to keep sharing them.

For example, you could consider opening a repository on Github, then you can keep adding files there, and only need to share it with interested people over here once, without the risk of being too spammy.

Anyways, good luck with your Rust learning journey.


(以下是机器翻译)

嗨,欢迎回来!正如我在你上一个话题的回复中已经解释过的:

你使用这个论坛的方式有些不符合它的预期用途。论坛的目的是用于讨论或解答问题。分享教程并讨论它们也是一个很好的用例(这也是你使用的“教程”标签通常适用的情况)。

如果你想收集一些小的代码示例,例如用于自己的学习,或者作为一个合集供他人参考,为每一个小示例都开一个新话题——而没有太多有意义的讨论前景——不幸的是,这在我们的论坛上可能会被认为有点像“刷屏”。因此,我将会取消列出你的一些话题,并且不得不请你寻找一种不同的方式来分享这些内容。

例如,你可以考虑在 Github 上开一个仓库,这样你就可以在那里不断添加文件,然后只需在这里与感兴趣的人分享一次,这样就不会有刷屏的风险了。

总之,祝你在学习 Rust 的过程中好运。

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.