1 00:00:06,030 --> 00:00:12,240 Splitting the computation of a program into multiple threads might improve performance since we are 2 00:00:12,240 --> 00:00:15,600 taking advantage of multiple tasks at the same time. 3 00:00:15,870 --> 00:00:18,810 But with this comes increased complexity. 4 00:00:19,020 --> 00:00:25,470 We might run into issues like race conditions where threads are accessing data or resources inconsistently. 5 00:00:26,420 --> 00:00:30,870 Or deadlocks which occur when two threads are waiting for each other to finish. 6 00:00:30,890 --> 00:00:33,350 Therefore, neither thread finishes. 7 00:00:34,100 --> 00:00:37,910 But we will dive into those concepts a little later on in this section. 8 00:00:37,910 --> 00:00:40,580 So for now, let's look at how we can create a thread. 9 00:00:41,210 --> 00:00:48,200 The first thing we need to do is bring it in to scope, which is in standard thread. 10 00:00:49,310 --> 00:00:53,870 And now to create a thread we just use thread spawn. 11 00:00:54,200 --> 00:01:02,810 And then for now we just want our thread to print out a message and we'll say hello from a thread. 12 00:01:05,840 --> 00:01:07,100 Now let's run this. 13 00:01:09,310 --> 00:01:11,440 And we'll see that nothing was printed out. 14 00:01:11,710 --> 00:01:13,180 But then we run it again. 15 00:01:14,070 --> 00:01:16,080 And hello from a thread was printed out. 16 00:01:16,770 --> 00:01:19,980 And then if we run it a third time, nothing was printed out. 17 00:01:20,220 --> 00:01:22,620 So why is this happening? 18 00:01:22,650 --> 00:01:28,890 Well, it's because the main thread is actually finishing before our thread spawns and is able to print 19 00:01:28,890 --> 00:01:29,990 out our message. 20 00:01:30,000 --> 00:01:37,680 So what we can do to just make sure that we give our thread time to be created and then print out our 21 00:01:37,680 --> 00:01:38,670 message for us. 22 00:01:38,820 --> 00:01:47,160 We will use another crate called duration and what duration is going to do for us? 23 00:01:47,180 --> 00:01:51,060 It's going to allow us to make our main thread. 24 00:01:52,050 --> 00:01:52,790 Sleep. 25 00:01:52,800 --> 00:01:54,000 So we'll say. 26 00:01:55,170 --> 00:02:01,620 Thread sleep duration from seconds and then we'll say one. 27 00:02:01,620 --> 00:02:05,490 So we just want our main thread to sleep for one second. 28 00:02:05,820 --> 00:02:09,390 So now if we run it, we see hello from a thread ran. 29 00:02:09,390 --> 00:02:14,940 But then you can also see that there is a little pause and that pause is our thread sleeping for one 30 00:02:14,940 --> 00:02:15,600 second? 31 00:02:16,080 --> 00:02:21,750 Well, this isn't actually the best way to do this, so there is a better way of doing it. 32 00:02:21,750 --> 00:02:27,210 And it is by using the join method so we can actually delete all of this. 33 00:02:28,460 --> 00:02:31,190 And we see that spawn returns a. 34 00:02:32,310 --> 00:02:40,680 Boeing handle so we can say let handle equal and then the joint handle that gets returned and now we 35 00:02:40,680 --> 00:02:48,510 can say handle dot join, dot wrap. 36 00:02:50,790 --> 00:02:54,630 And what join is is it's a form of blocking. 37 00:02:54,630 --> 00:03:00,690 And blocking a thread means that the thread stops running until the thread represented by the handle. 38 00:03:00,690 --> 00:03:02,940 Join terminates. 39 00:03:02,940 --> 00:03:03,600 Excuse me. 40 00:03:03,600 --> 00:03:04,110 Join. 41 00:03:04,110 --> 00:03:04,710 Handle. 42 00:03:05,960 --> 00:03:12,920 So we're going to block our main thread until our thread that we is. 43 00:03:13,820 --> 00:03:14,660 Excuse me. 44 00:03:14,660 --> 00:03:18,620 Until our joint handle that we assign to handle. 45 00:03:19,780 --> 00:03:23,890 Is finished executing, and that is what Jordan is going to do for us. 46 00:03:23,890 --> 00:03:27,700 So now we can say we'll say print line 47 00:03:30,280 --> 00:03:32,350 hello from main. 48 00:03:34,490 --> 00:03:40,040 And now if we run this, we see that we got hello from thread and then hello from Maine. 49 00:03:40,220 --> 00:03:40,970 So. 50 00:03:41,780 --> 00:03:51,110 This will always pan out prior to this because we told our main thread to wait until this thread has 51 00:03:51,110 --> 00:03:52,490 completed its task. 52 00:03:53,840 --> 00:03:59,150 So now let's move into a little bit more in depth reading. 53 00:04:02,310 --> 00:04:07,440 So let's create a vector and we'll just call it V and we'll assign it the values. 54 00:04:07,440 --> 00:04:09,210 One, two and three. 55 00:04:11,280 --> 00:04:14,310 And then we want to spawn another thread. 56 00:04:14,970 --> 00:04:19,680 And for now, we will move this forward out of here. 57 00:04:20,970 --> 00:04:24,390 And now we want to print out our vector. 58 00:04:33,320 --> 00:04:35,270 Well, we see that we get an air. 59 00:04:36,760 --> 00:04:43,420 And our air says that our closure, which you can see that we have denoted here, may outlive the current 60 00:04:43,420 --> 00:04:49,960 function, but it borrows our vector V, which is owned by the current function. 61 00:04:50,440 --> 00:04:57,160 So that word that we originally got rid of, we can now put it back in and it is called Move. 62 00:04:57,850 --> 00:05:05,020 And Move is going to allow us to force the closure to take ownership of the values it uses. 63 00:05:05,560 --> 00:05:07,690 So now if we try to run this, 64 00:05:10,750 --> 00:05:18,190 it runs flawlessly because we told our closure to take ownership of the V vector. 65 00:05:19,920 --> 00:05:25,800 So now let's look at how we can spawn multiple threads. 66 00:05:27,400 --> 00:05:33,070 So we'll keep our current vector and then we'll create another vector that's going to handle. 67 00:05:36,220 --> 00:05:38,560 All the threads that we spawn. 68 00:05:39,550 --> 00:05:44,860 So I'm going to comment this out since we want to use our vector again. 69 00:05:47,010 --> 00:05:48,540 But now we can say. 70 00:05:51,620 --> 00:05:52,520 For. 71 00:05:53,630 --> 00:05:58,100 E element an RV vector. 72 00:05:58,820 --> 00:06:00,200 We want thread. 73 00:06:02,580 --> 00:06:09,030 No thread handles and we want to push in spawning. 74 00:06:17,670 --> 00:06:18,570 Vectors in. 75 00:06:28,580 --> 00:06:31,460 Now, let's just format this to make it look a little better. 76 00:06:33,880 --> 00:06:36,140 All right, so what are we doing here? 77 00:06:36,160 --> 00:06:39,310 So we created a new vector called thread handles. 78 00:06:39,580 --> 00:06:42,700 And for every element inside our vector. 79 00:06:42,700 --> 00:06:47,050 So one, two and three, we are going to push in the result. 80 00:06:47,050 --> 00:06:48,010 So a. 81 00:06:49,080 --> 00:06:49,950 Join handle. 82 00:06:49,980 --> 00:06:54,510 We're going to push in a join handle into our thread handles vector. 83 00:06:54,510 --> 00:06:59,190 So what this is going to do, it's going to create three vectors for us. 84 00:07:00,690 --> 00:07:01,740 And they're going to be running. 85 00:07:01,740 --> 00:07:06,800 So while they're running, I want my main function to print. 86 00:07:07,650 --> 00:07:08,880 Main thread. 87 00:07:09,120 --> 00:07:09,900 Excuse me. 88 00:07:09,900 --> 00:07:13,380 Our main I want our main thread to print out that it's the main thread. 89 00:07:13,530 --> 00:07:19,230 And now for handle and thread handles, we need them to join. 90 00:07:20,640 --> 00:07:28,860 That way we make sure that they finish executing and print out the thread that they are. 91 00:07:29,280 --> 00:07:33,840 So we'll run this and we see we got main thread thread. 92 00:07:33,840 --> 00:07:35,400 One, two, three. 93 00:07:36,490 --> 00:07:43,690 And now we see we have main thread thread three or excuse me, thread two, thread one and thread three. 94 00:07:43,690 --> 00:07:48,430 So the order that you might see may very differently than mine. 95 00:07:50,260 --> 00:07:57,400 But it's a good way to show that certain threads might finish executing, not in the order that they 96 00:07:57,400 --> 00:07:58,600 were spawned up in. 97 00:07:59,770 --> 00:08:02,290 So I just wanted to point that out to you. 98 00:08:03,270 --> 00:08:09,660 And this lecture was just a simple demonstration on how to spawn a thread, how to join the threads, 99 00:08:09,660 --> 00:08:15,990 and also how to use the mov keyword to force a closure to take ownership of the values. 100 00:08:15,990 --> 00:08:22,230 Because all of this will be important as we continue on learning more about threads as we continue in 101 00:08:22,230 --> 00:08:22,920 this section.