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 #rustlang #tutorial
Пікірлер: 36
📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
I was expecting that to be more complicated.. nice 👍🏻
@magnoelmagnifico957
2 жыл бұрын
me too haha
@jonathanmoore5619
2 жыл бұрын
That's the next part...
@everyhandletaken
2 жыл бұрын
@@jonathanmoore5619 calm before the storm ☺️
@veirt
2 жыл бұрын
Ikr, the chapter before (smart pointers) was hell for me who came from high level languages
@everyhandletaken
2 жыл бұрын
@@veirt I can relate, most things are hell for me 😂
Just 15 videos to go. You got this
Very useful & clear on why we need to move ownwership into the closure 👍🏻Thank you
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
Good work mate, thank you for these videos they're very good. Keep up the good work.
Keep it up , man !!! awesome as always
Sweet stuff! Thanks, man!
Eagerly awaiting your video on async await and futures
Thank you very much for this clear video. I learned so much.
Thank you! 👍
This is amazing
this is very interesting!! nice explanation!
Really useful!
eagerly waiting for listening to the async by you
@aqua3418
2 жыл бұрын
Don't you mean you're _awaiting_ for him to explain it? **nudge nudge**
great video - thanks! do u plan to cover async rust at some point?
@letsgetrusty
2 жыл бұрын
Yes!
Can you make a video about scoped threads? New in 1.63.
Please make a video on using Tokio
@letsgetrusty
2 жыл бұрын
I will eventually!
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-.-
Жыл бұрын
std::sync::Arc
How is race condition and dead lock. prevented in this example?
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
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
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
Жыл бұрын
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"
"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
I thought this video was alright. I wish you did more than just rehash the textbook though. Some different examples would have helped.
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.