Concurrency in Rust - Creating Threads

Ғылым және технология

The ultimate Rust lang tutorial. Follow along as we go through the Rust lang book chapter by chapter.
📝 Get notified when the Rust Cheatsheet comes out: letsgetrusty.com/cheatsheet
The Rust book: doc.rust-lang.org/stable/book/​​
Chapters:
0:00​ Intro
1:21 Using Threads
4:24 Creating a New Thread with spawn
6:32 Using Join Handles
7:56 Using move Closures with Threads
10:13 Outro
#letsgetrusty​​ #rust​lang​ #tutorial

Пікірлер: 36

  • @letsgetrusty
    @letsgetrusty2 жыл бұрын

    📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet

  • @everyhandletaken
    @everyhandletaken2 жыл бұрын

    I was expecting that to be more complicated.. nice 👍🏻

  • @magnoelmagnifico957

    @magnoelmagnifico957

    2 жыл бұрын

    me too haha

  • @jonathanmoore5619

    @jonathanmoore5619

    2 жыл бұрын

    That's the next part...

  • @everyhandletaken

    @everyhandletaken

    2 жыл бұрын

    @@jonathanmoore5619 calm before the storm ☺️

  • @veirt

    @veirt

    2 жыл бұрын

    Ikr, the chapter before (smart pointers) was hell for me who came from high level languages

  • @everyhandletaken

    @everyhandletaken

    2 жыл бұрын

    @@veirt I can relate, most things are hell for me 😂

  • @sonicsplasher
    @sonicsplasher4 ай бұрын

    Just 15 videos to go. You got this

  • @exoticcoder5365
    @exoticcoder53652 жыл бұрын

    Very useful & clear on why we need to move ownwership into the closure 👍🏻Thank you

  • @yousefh4939
    @yousefh49392 жыл бұрын

    i will rewatch all the videos after it get complete it make me happy to see you made new video every time i log in youtube

  • @aloysberger9482
    @aloysberger94822 жыл бұрын

    Good work mate, thank you for these videos they're very good. Keep up the good work.

  • @herrxerex8484
    @herrxerex84842 жыл бұрын

    Keep it up , man !!! awesome as always

  • @ClaudioParraGonzalez
    @ClaudioParraGonzalez2 жыл бұрын

    Sweet stuff! Thanks, man!

  • @kishanbsh
    @kishanbsh2 жыл бұрын

    Eagerly awaiting your video on async await and futures

  • @func0der
    @func0der2 жыл бұрын

    Thank you very much for this clear video. I learned so much.

  • @emvdl
    @emvdl2 жыл бұрын

    Thank you! 👍

  • @alanaxotla9112
    @alanaxotla91122 жыл бұрын

    This is amazing

  • @mybdretaemch3409
    @mybdretaemch34095 ай бұрын

    this is very interesting!! nice explanation!

  • @mjovanc
    @mjovanc9 ай бұрын

    Really useful!

  • @----__---
    @----__---2 жыл бұрын

    eagerly waiting for listening to the async by you

  • @aqua3418

    @aqua3418

    2 жыл бұрын

    Don't you mean you're _awaiting_ for him to explain it? **nudge nudge**

  • @MrGreen-kq4ds
    @MrGreen-kq4ds2 жыл бұрын

    great video - thanks! do u plan to cover async rust at some point?

  • @letsgetrusty

    @letsgetrusty

    2 жыл бұрын

    Yes!

  • @jabuci
    @jabuci Жыл бұрын

    Can you make a video about scoped threads? New in 1.63.

  • @saadabbasi2063
    @saadabbasi20632 жыл бұрын

    Please make a video on using Tokio

  • @letsgetrusty

    @letsgetrusty

    2 жыл бұрын

    I will eventually!

  • @spongechameleon6940
    @spongechameleon69402 жыл бұрын

    Is there an idiomatic way to pass the same data in heap to multiple threads for reads? Like by wrapping the data in Rc?

  • @cat-.-

    @cat-.-

    Жыл бұрын

    std::sync::Arc

  • @TON-vz3pe
    @TON-vz3pe Жыл бұрын

    How is race condition and dead lock. prevented in this example?

  • @mdrealiyev
    @mdrealiyev10 ай бұрын

    Hello. Evertig is good and understandable. But I have a question. How can I call thread::spawn inside implemented class and pass self safety?

  • @KokahZ777

    @KokahZ777

    20 күн бұрын

    I pattern I found is to wrap your struct (call it A) data in some Inner struct that is Arc in A That way you can pass a self clone to your functions which just duplicates the pointer but not the actual data

  • @emilfilipov169
    @emilfilipov169 Жыл бұрын

    I find this confusing and I can't find anyone who has explained it yet. So, the thread takes ownership of your variable, say v, for example. You "can't" use it outside of the thread, but yet you can. fn main() { let limit: i32 = 5; let handle = thread::spawn(move || { for n in 1..=limit { println!("Spawned, {}", n * n); thread::sleep(Duration::from_millis(1)) } }); for n in 1..=limit { println!("Main, {}", n * n); thread::sleep(Duration::from_millis(1)) } handle.join().unwrap(); } Why does this code run? If one thread has ownership of the variable, doesn't that mean nothing else can own it?

  • @kylezwarich

    @kylezwarich

    Жыл бұрын

    The move keyword for closure tells Rust compiler to keep "limit" in scope for the duration of the spawned thread, and handle.join().unwrap() says to run all threads through to completion. So every loop iteration the spawned thread asks "is limit still a thing", Rust says "it is a thing because you still need it"; even though the main thread might have finished processing its batch of loops already, the other still needs access to 'limit'. If me and you order spaghetti and meatballs, but I hate noodles and you hate meatballs, we can split the dish and we both have a satisfying meal, but we have to share the fork. Just because I'm finished all the meatballs doesn't necessarily mean our dinner is done--using the move keyword and the thread::join() says "we both need the fork so don't get rid of it until we're both done the plate"

  • @janedoe6182
    @janedoe618210 ай бұрын

    "As soon as you type new Thread(), it’s over; your project already has legacy code" (c) Stephen Cleary In general creating new OS threads in modern programs - bad idea. Except specialized pools serving async I/O

  • @hobbes5043
    @hobbes50432 жыл бұрын

    I thought this video was alright. I wish you did more than just rehash the textbook though. Some different examples would have helped.

  • @alexisgavidia3141
    @alexisgavidia314111 ай бұрын

    hi, i have a question if !l.iter().any(|x| x.file_name == j.file_name) { //copy file to tmp folder. let y = j.clone(); let ha = thread::spawn(move || async move { cp_to_tmp_and_pod( cmd_start_raw_capture_ecs, y, i, pods, n, &path_on_the_host, ) .await; }); n += 1; l.push(j); ha.join().unwrap().await; } } ha.join().unwrap().await; i cant run it the function wiout the await. and i want to run but the main thred need to wait until this fisnih.

Келесі