1 00:00:06,000 --> 00:00:06,450 Message. 2 00:00:06,450 --> 00:00:09,150 Passing is not the only way we can communicate. 3 00:00:09,450 --> 00:00:11,700 We can use new taxes. 4 00:00:12,000 --> 00:00:21,330 A mutex is a shortened name for mutual exclusion and only allows one thread access to some data at any 5 00:00:21,330 --> 00:00:22,200 given time. 6 00:00:22,530 --> 00:00:26,580 To allow only one thread access to the data, there is a lock. 7 00:00:27,920 --> 00:00:35,330 Alok is a part of the mutex data structure that keeps track of who currently has exclusive access to 8 00:00:35,330 --> 00:00:35,990 the data. 9 00:00:37,040 --> 00:00:39,770 When the thread says, Hey, I want that data. 10 00:00:40,960 --> 00:00:47,950 It must ask for the mutex as lock, and then once the lock is acquired, nobody else can use that data 11 00:00:47,950 --> 00:00:49,780 until the lock is given back. 12 00:00:50,230 --> 00:00:52,090 There are two rules to follow. 13 00:00:53,020 --> 00:00:56,920 You must acquire the lock before using the data. 14 00:00:56,920 --> 00:01:05,320 And when you're done with the data that the mutex is guarding, you must give the lock back so that 15 00:01:05,320 --> 00:01:09,940 it is unlocked for every other thread to be able to obtain access. 16 00:01:10,030 --> 00:01:16,470 So now let's see how we can use mutex and threads for the example. 17 00:01:16,480 --> 00:01:24,310 Remember that arc is handy for sharing things across threads and that a mutex is handy for mutating 18 00:01:24,310 --> 00:01:26,680 data that is shared across threads. 19 00:01:27,530 --> 00:01:30,290 So the first thing we want to do is bring. 20 00:01:33,030 --> 00:01:38,690 The art smart pointer and a mutex into scope. 21 00:01:38,970 --> 00:01:42,870 So we'll bring ark and mutex into scope. 22 00:01:45,860 --> 00:01:53,330 Now we're going to create a counter and we'll make it an arc that contains. 23 00:01:54,880 --> 00:01:56,970 Mutex in the value. 24 00:01:58,000 --> 00:02:00,340 Is going to be zero. 25 00:02:03,150 --> 00:02:07,320 And now we're going to create a handle's vector. 26 00:02:08,880 --> 00:02:13,950 That's going to store all the join handles we create. 27 00:02:14,700 --> 00:02:20,190 So now we're going to have a little loop and we don't care about any of the values inside. 28 00:02:20,190 --> 00:02:24,930 So we'll say four 0 to 10 and actually I'll make it eight. 29 00:02:26,750 --> 00:02:31,700 We'll say let counter equals arc clone. 30 00:02:32,790 --> 00:02:35,760 And then a reference to our counter variable. 31 00:02:38,570 --> 00:02:49,340 And now we'll say let handle equal threats bond and we'll say let mutable num equals counter. 32 00:02:49,460 --> 00:02:56,180 Now we have to lock it to gain access to the counter and we want to unwrap the value inside. 33 00:02:57,680 --> 00:03:01,130 The reference, the number and add one to it. 34 00:03:01,130 --> 00:03:05,900 And that's how we're going to get our calendar to go from 0 to 1, two, three, four, five, six, 35 00:03:05,900 --> 00:03:06,440 seven, eight. 36 00:03:08,390 --> 00:03:14,750 And once we're done with that, obviously we need to push. 37 00:03:15,830 --> 00:03:17,600 Our handle into our vector. 38 00:03:19,260 --> 00:03:25,110 For now for us to join our threads. 39 00:03:25,740 --> 00:03:36,000 So for handle and handles, handle, dot, join and then it respects it expects a result. 40 00:03:42,920 --> 00:03:44,390 And now we can print out. 41 00:03:49,500 --> 00:03:50,960 Counter Dot. 42 00:03:52,500 --> 00:03:56,850 Lock, dot and wrap. 43 00:03:56,910 --> 00:03:59,400 That way we can see our final count. 44 00:03:59,910 --> 00:04:02,970 And if we run this, we see eight is printed out. 45 00:04:05,710 --> 00:04:13,090 Now that we see kind of how mutex is work in a sense, let's see what would happen if we created a. 46 00:04:13,830 --> 00:04:14,490 Headlock. 47 00:04:15,540 --> 00:04:23,760 So now we'll say let mutable num two equals to counter dot lock 48 00:04:26,250 --> 00:04:27,160 unwrap it. 49 00:04:27,180 --> 00:04:32,310 So now here we're saying, hey, I want we're giving the lock to num. 50 00:04:33,160 --> 00:04:37,080 But now we have number two also requesting that lock. 51 00:04:37,090 --> 00:04:40,090 And what this is going to do is it's going to prevent. 52 00:04:41,280 --> 00:04:41,940 This. 53 00:04:42,940 --> 00:04:45,340 From ever being able to execute. 54 00:04:46,170 --> 00:04:46,590 Now. 55 00:04:47,570 --> 00:04:55,610 Plus one because we're going to get stuck here as number two is waiting for the lock. 56 00:04:55,610 --> 00:05:01,940 But Num holds it, but NUM cannot give it up because it cannot finish running its execution. 57 00:05:01,940 --> 00:05:05,600 So we're just never going to be able to finish running this. 58 00:05:05,600 --> 00:05:11,180 And this is a really simple example of what a deadlock is. 59 00:05:12,000 --> 00:05:21,420 But it really demonstrates the purpose of a mutex and that data is unacceptable to other threads. 60 00:05:21,840 --> 00:05:26,160 While one thread contains a lock. 61 00:05:26,310 --> 00:05:27,180 And. 62 00:05:28,010 --> 00:05:34,370 So we'll we'll close this out and then obviously get rid of that to remove our deadlock. 63 00:05:35,540 --> 00:05:37,160 And again, we'll just run it. 64 00:05:37,910 --> 00:05:41,330 And what's happening is for every thread that we create. 65 00:05:42,680 --> 00:05:43,830 It's getting the lock. 66 00:05:43,850 --> 00:05:45,020 It's adding one. 67 00:05:45,230 --> 00:05:46,400 It gives up the lock. 68 00:05:46,430 --> 00:05:50,840 Ear lock is given up, and now the next thread gets it. 69 00:05:50,840 --> 00:05:52,040 And then the lock gives up. 70 00:05:52,040 --> 00:05:57,110 And it does this eight times for the eight threads that we are creating. 71 00:05:57,350 --> 00:05:58,130 So. 72 00:05:59,170 --> 00:06:01,060 That's really all there is to it. 73 00:06:01,990 --> 00:06:03,420 To concurrency. 74 00:06:03,430 --> 00:06:09,760 There are a couple of crates available to us called Cross Beam and ray on. 75 00:06:10,620 --> 00:06:16,740 And I encourage you to check those out now that you have a really good understanding of how concurrency 76 00:06:16,740 --> 00:06:25,230 works in rust, and you can check those crates out and see how they try to help simplify creating concurrent 77 00:06:25,230 --> 00:06:26,340 programs. 78 00:06:26,550 --> 00:06:33,810 But now that you have a great understanding of how concurrency works, let's continue on into asynchronous 79 00:06:33,810 --> 00:06:34,680 programming.