WEBVTT 1 00:00:02.790 --> 00:00:11.280 William Cheng: Welcome to lecture five. So we're at warm up to now so warm up to is due on Tuesday. 2 00:00:11.910 --> 00:00:18.029 William Cheng: June 16 as usual. If you have co from previous semester, don't look at them. Don't copy them best to get rid of it. 3 00:00:18.510 --> 00:00:28.920 William Cheng: And if you're new to multithreading you should start warm up to as early as possible. Okay. Grading guidelines. The only way we'll grey, we can all agree on 30 to build a bunch of 16.4 system. 4 00:00:29.670 --> 00:00:41.760 William Cheng: Don't install additional packages. The standard you know C plus plus aside the standard C library come with a piece or a library. So that's why you should stick to. And again, don't do anything too fancy. 5 00:00:42.660 --> 00:00:52.590 William Cheng: Run your code against the grading guidelines on the same system, the grading gun is part of the spec. Okay. And another reminder to form Colonel teams. 6 00:00:53.370 --> 00:00:59.310 William Cheng: So again, you should start with a bigger group work at a high level, with your potential partner form up to 7 00:00:59.820 --> 00:01:09.990 William Cheng: The team forming Delphi is the day after warm up to his do. Okay, so one more to do on Tuesday. So as on the Wednesday the data team forming their life. 8 00:01:10.350 --> 00:01:15.330 William Cheng: After that, so please read the cost website to find a higher how I would form. 9 00:01:15.960 --> 00:01:25.260 William Cheng: You know teams with the remaining student right so if you are a student that wants to be in a team and you don't belong to any team then in that case, I'm going to take four of them together and put them into a random team. 10 00:01:25.800 --> 00:01:33.450 William Cheng: So, so again. So in that case, you are not joining and the existing team. You are forming a team with people that don't have a team. 11 00:01:33.930 --> 00:01:39.510 William Cheng: Okay, so again the class web page spells out exactly what I'm going to do. Okay, so, so read that procedure. 12 00:01:40.350 --> 00:01:48.000 William Cheng: So what I typically recommend people to do it is that you shouldn't be working with people you never have worked with before, because I always 13 00:01:48.690 --> 00:01:56.190 William Cheng: You know, in the past, it always has really really big problem, you know, because people don't respond to your email people don't, you know, answer your text. 14 00:01:56.670 --> 00:02:03.870 William Cheng: People don't do anything. There's nothing you can do. Yeah. And also there's nothing I can do. Because I don't get involved, you know in in the 15 00:02:04.230 --> 00:02:12.090 William Cheng: In the team forming process. If I end up forming a random team at the random number generator is taking care of it i don't i don't really get involved. Okay. 16 00:02:12.570 --> 00:02:19.470 William Cheng: So, so if you if you end up. You never you don't have a sort of partner to form a team with and the and you're going to get any 17 00:02:19.890 --> 00:02:32.310 William Cheng: Formula random team with people that you never worked with before my recommendation is that you should do all your kernel assignment by yourself. Okay. But if you still chooses to work with people you don't know then then then that's completely up to you. 18 00:02:33.360 --> 00:02:39.240 William Cheng: OK. So again, you should try to form team now use the class website to do that and 19 00:02:40.770 --> 00:02:48.720 William Cheng: All right, the tentative timeline for warm up to as a warm up to, if you read the space. It says going to have to handle control. See, you don't know how to handle control see it. 20 00:02:49.350 --> 00:02:53.850 William Cheng: So what I would ask you to do is to focus on getting the simulation to be perfect. 21 00:02:54.120 --> 00:03:02.670 William Cheng: And then in the end. Add the control see handling code. Okay, there's plenty of time to edit control see calico and it comes to see handling code can be done very, very quickly and very easily. 22 00:03:03.180 --> 00:03:19.830 William Cheng: Okay, you just have to wait a couple of lectures and then you'll be ready to do that. Okay, so, so think they should do. He is to write a nice and clean program, guys. So one thing that will recommend you to do is that you make the first procedure of every thread, you know, nice and simple. 23 00:03:21.240 --> 00:03:29.700 William Cheng: OK. So again, the goal is to look at the code is a disco has no bugs. Right. So, so, you know, so what, so. So in that case, what, you know, what is the first procedure is supposed to do. 24 00:03:29.910 --> 00:03:36.780 William Cheng: The first episode is procedure supposed to call other functions. And if you look at other function. And again, it's nice and simple. So, therefore, you say, Well, you look at is. There's no bug. 25 00:03:37.290 --> 00:03:47.850 William Cheng: So eventually, you know, all the function, they will do that they will call nice and simple function. They're all well divine. So this way you're going to end up with very, very little books. Okay. So that would be my recommendation. Okay. 26 00:03:48.960 --> 00:03:58.380 William Cheng: The program. The warm up to FAQ has a lot of suggestion. So make sure you follow the sort of the suggestion there and write your code that way. Yeah. 27 00:03:59.760 --> 00:04:04.470 William Cheng: And, you know, one thing that that would recommend is that you only use one new tax. 28 00:04:04.920 --> 00:04:12.930 William Cheng: To lock and unlock the entire Token Bucket filter right as a warm up to you have implemented this big data structure called Token Bucket filter and it's complicated. 29 00:04:13.530 --> 00:04:23.220 William Cheng: So what I will recommend you to do is to put all the global to put all the shared variable inside the serialization box. Okay, so this way any thread that wants to, you know, 30 00:04:24.210 --> 00:04:31.470 William Cheng: Want to read the value of these these variables or they want to modify the value of these variable. They all have to do this with the new tax law. 31 00:04:32.310 --> 00:04:39.210 William Cheng: Okay, so in that case you are so as I mentioned at the end of the last lecture. If we do like that you are losing parallelism. 32 00:04:39.690 --> 00:04:44.400 William Cheng: Okay, so in our programming for warm up to It's Okay To lose penaloza 33 00:04:44.820 --> 00:04:54.450 William Cheng: Okay, because because warm up to is actually a multi threading exercise. It's not a new tax exercise. Okay. If I want to give you a meat AX exercise. I'm going to give you some exercise with 100 new tags. 34 00:04:54.840 --> 00:05:02.580 William Cheng: Okay. But in this case, this is that that's not the kind of the access that we need to learn in this class, we need to learn how multi multiple threads work with each other. 35 00:05:02.820 --> 00:05:04.890 William Cheng: Okay, that's what you need to get out of a warm up to 36 00:05:05.190 --> 00:05:15.240 William Cheng: Yeah, so please just use a single new tags and today we're going to talk about something, also called condition variable. So you're going to use one thing one mutates and one condition variable and don't try to do anything fancy 37 00:05:15.570 --> 00:05:26.490 William Cheng: OK. So, again, that will be my recommendation. Yeah, if you can really think about how to get this done. The Akaka. How do I program this way. Just send me email, right, and started discussion, the cosmic go group. Yeah. 38 00:05:27.210 --> 00:05:34.200 William Cheng: Alright, so you should get the simulation to work during the first week you know from this, you know, I guess we started on on Tuesday. 39 00:05:34.830 --> 00:05:42.960 William Cheng: So all the way to the next Tuesday or even to Thursday, you should be working on the simulation. Okay, to get the simulation to work perfectly. It's going to take you some time. 40 00:05:43.260 --> 00:05:46.680 William Cheng: Okay, and then we worry about taking care of the control see a little later. Okay. 41 00:05:47.490 --> 00:05:52.140 William Cheng: All right, all right. If you're new to multi threading, you should write tiny programs to test out your ideas. 42 00:05:52.380 --> 00:05:58.350 William Cheng: Debug those tiny program to see what happened when you set a breakpoint. You know how how do all these things interact with each other. 43 00:05:58.590 --> 00:06:05.010 William Cheng: That's the point of this exercise, you have to learn how to how to write mathematical and then to understand their interaction. Yeah. 44 00:06:05.760 --> 00:06:16.680 William Cheng: The lecture today should cover everything that you need to to get the simulation going okay and the next lecture will sort of talk about how to handle control. See how to handle signals. 45 00:06:17.280 --> 00:06:26.820 William Cheng: Right, so we press Control. See you going to get a signal we talked about the signal is up call that kind of stuff in chapter one. So now we're going to look a little more detail in the next lecture about how to handle control, see, okay. 46 00:06:29.220 --> 00:06:36.540 William Cheng: All right, so let's go back to where we were before. So we were, you know, talking about 47 00:06:37.680 --> 00:06:44.550 William Cheng: You know God garden command. Right. So God commands abstraction, we talked about how to use garden command to implement the P and V operation. 48 00:06:45.150 --> 00:06:57.150 William Cheng: You know, again, his quick review right P operation is the atomic detriment operation and debris operations atomic increment operation. They are both implemented using a garden command. Okay, so when we think about P amp D. 49 00:06:57.570 --> 00:07:01.350 William Cheng: They are, you know, we should think about, you know, we can, we should think about the garden command. 50 00:07:02.700 --> 00:07:06.810 William Cheng: Okay, it's kind of weird that I have the wrong color here. Okay. All right. 51 00:07:07.680 --> 00:07:14.760 William Cheng: So, so, you know, so, so I guess toward the end of last lecture, I mentioned that there's one producer threat. There's one consumer threat right 52 00:07:14.940 --> 00:07:19.500 William Cheng: Producing consumer over here, when they tried to synchronize with each other, they will execute this code. 53 00:07:19.710 --> 00:07:24.810 William Cheng: Okay, so this code is synchronization code. This code is not the first procedure of all of these two thread. 54 00:07:25.050 --> 00:07:33.720 William Cheng: These two threads are usually do something else. Once in a while when they need to produce when they need to console, they call these to function to produce or to consume. Okay. 55 00:07:34.560 --> 00:07:39.630 William Cheng: Alright, so let's take a look at the code briefly over here for the pursuit producer. So what the producer. 56 00:07:40.500 --> 00:07:49.170 William Cheng: will do is that they will perform a p operation on the empty samovar right the empty stomach. Over here is initialized to be the number of slots on the conveyor belt. 57 00:07:49.410 --> 00:08:00.000 William Cheng: Okay, so let's say that this is your computer about over here is a b equals eight. Right. So in this case, the conveyor belt over here has a slots. So again, I'm going to use a circular link a double link circular list to implement this. 58 00:08:00.630 --> 00:08:08.280 William Cheng: So I wouldn't want a spot is empty, you can put it in anywhere. We're going to use these two variables over here to control where to put data in order to take data out. 59 00:08:08.520 --> 00:08:14.520 William Cheng: Okay, so next thing is an array index next equal to zero, that means that you're putting data into array index number zero 60 00:08:14.820 --> 00:08:21.240 William Cheng: Okay, next hour over here is also equal to zero, that means that you know that the data you need to work on is also at a rate that's number zero 61 00:08:22.200 --> 00:08:29.460 William Cheng: Okay, so, so let's take a look at the producer code, the producer code when they are ready to produce any to put data into location zero over here instead of buffer. 62 00:08:29.610 --> 00:08:38.580 William Cheng: So, therefore, what should you do right so number why you need to perform the operation of empty right now empty equals to A. So, therefore, you can attack from either atomic we documented to seven. 63 00:08:38.790 --> 00:08:41.370 William Cheng: And now you're allowed to come to execute the code in the middle over here. 64 00:08:41.760 --> 00:08:51.780 William Cheng: Okay, so the data that you try to produce over here. I'm going to use the example of what single character that. So, therefore, we're going to put this character into the array index number zero, right. So this is an array of 65 00:08:52.020 --> 00:08:58.530 William Cheng: Characters. So we're gonna say buff Nexium equal to item right above next in an x equal to zero, we're going to put the data right here. 66 00:08:58.800 --> 00:09:04.950 William Cheng: Okay, and then we say next equal to the next in the plus one. So we're going to get ready for the next time we come to produce. So we're going to 67 00:09:05.220 --> 00:09:07.380 William Cheng: Win increment next into point to the next guy over here. 68 00:09:07.650 --> 00:09:15.990 William Cheng: And then we're going to do a wrap around check in case that you know next thing is equal to be over here, which is the yet that in this case you need to wrap it around, because we're implementing a circular us 69 00:09:16.680 --> 00:09:24.270 William Cheng: Okay, so next thing will be go to zero. If you are if you reach the end and then finally what perform a V operation. So I mentioned before. 70 00:09:24.780 --> 00:09:33.960 William Cheng: You know, typically, when we see a similar form being used. The three I will perform P operation on 174 and he will. He will perform the operation on a different samovar 71 00:09:34.500 --> 00:09:43.650 William Cheng: Okay, so here's an example. Right. One of the p operation on the empties time before and we use a V operation or the Occupy some of war as Africa occupied several for original equal to zero. 72 00:09:44.010 --> 00:09:50.880 William Cheng: Okay, so now we're going to automatically increase it to want to say that now one of the slots inside this conveyor belt over here now has been occupied 73 00:09:51.960 --> 00:09:57.420 William Cheng: Okay, so when you return for this function, the number of empty spot is equal to seven and the number of Occupy sparks equal to one. 74 00:09:58.260 --> 00:10:03.840 William Cheng: Okay, so when this equal to one. Now, the consumer can actually work on it. So the consumer over here the consumers that comes 75 00:10:04.560 --> 00:10:12.780 William Cheng: Now, the producer was written about this function. So what we'll do is we'll go to whatever it does. And eventually, when it, when it create another character you will call the produce function to produce a character. 76 00:10:13.620 --> 00:10:18.150 William Cheng: Yeah. Oh, sorry. It will call produce the put the character into the buffer right here. Yeah. 77 00:10:18.720 --> 00:10:28.410 William Cheng: The consumers one's going to take out a character to work on it. Okay, so what it will do is that you will create a local variable called item over here. And then he will perform a p operation on the number of occupied item. 78 00:10:28.920 --> 00:10:35.040 William Cheng: So the idea here is that if occupy equal to zero. Well, then, that the consumer has no nothing to work on. Right. So in that case, 79 00:10:35.280 --> 00:10:43.680 William Cheng: This function over here will block because the PR, you know, the PRP operations is implemented this way, right, if x is equal to zero, then you get stuck here forever. 80 00:10:44.310 --> 00:10:50.190 William Cheng: Okay, so in this case the number occupied over here is going to equal to one. So, therefore, you automatically document into zero 81 00:10:50.400 --> 00:10:57.360 William Cheng: And then what you do is that you go to the buff. Next, our next are equal to zero. So you're gonna take this character over here and you're going to copy into this local variable. 82 00:10:58.620 --> 00:11:03.450 William Cheng: Okay, so in order for you to take data out. You need to put it somewhere. So right now, we'll put it, we'll put into the local variable right here. 83 00:11:03.660 --> 00:11:08.280 William Cheng: And then we say next cycle, an x plus y. We're going to get it ready for the next time you got to call this function and 84 00:11:08.520 --> 00:11:14.850 William Cheng: We do the wraparound check and then we perform the V operation on the empty Sam of war and now the empty. 85 00:11:15.150 --> 00:11:23.190 William Cheng: The number empty star should be equal to eight and the net number occupies by equal to zero. And now we're going to return the item over here using this function. 86 00:11:23.430 --> 00:11:33.390 William Cheng: Right. So we first we copied this data into a local variable and then we return from that. So the idea here is that the consumer threat will call it will call this function to return a character from the conveyor belt. 87 00:11:35.340 --> 00:11:43.620 William Cheng: Okay, so, so, so, so in this case, you know, kind of the consumer of your return and then it will take this character and he will work on it and eventually when it finished consuming that character. 88 00:11:43.830 --> 00:11:48.300 William Cheng: It will call the consumer function over here to try to take on another character on the conveyor belt. 89 00:11:49.560 --> 00:11:52.530 William Cheng: Okay, so that's the idea of the of the to function. 90 00:11:53.130 --> 00:12:03.300 William Cheng: So I guess we can, I guess I pretty much went through the code over there. So if we simulate the code again you know next n equal to zero and x is equal to zero empty code is a occupy equal to zero. 91 00:12:03.720 --> 00:12:09.810 William Cheng: When we start executing as a scope, it is possible. We have two different CPU and we can execute these two coasts simultaneously right 92 00:12:10.140 --> 00:12:18.630 William Cheng: Now, if we execute them simultaneously for the consumer, the number occupied over here, equal to zero. So, therefore, the consumer is going to go the consumer is going to get blocked. 93 00:12:19.290 --> 00:12:25.620 William Cheng: Okay, so remember the P operation over here is a blocking call. So the in this case since occupy equal to zero, the consumer will get blocked. 94 00:12:26.010 --> 00:12:36.930 William Cheng: The producer cannot you go ahead, what he would do is that it would detriment. The empty stomach for using the atomic operation. So, this one will go to seven. Right. And then what is the next thing I will do is that it will 95 00:12:37.590 --> 00:12:44.580 William Cheng: Take the data over here and then put into slot number zero right here. So I'm going to sort of change this into a different color to say there's data in there now. 96 00:12:44.970 --> 00:12:49.890 William Cheng: Okay, and then we're going to increment next internet same plus y. So, the producers. The next work on the next 97 00:12:50.130 --> 00:12:56.760 William Cheng: The next slide right here. So they will look like this. Do the wraparound check is not at the end over here. So, therefore, nothing happened. 98 00:12:56.940 --> 00:13:04.920 William Cheng: And now I'm going to perform the operation on occupies occupy overhears good again from an equal to one, right. Again, this is exactly what I just went through right and now producer is going to be 99 00:13:05.100 --> 00:13:10.920 William Cheng: And now, at this time, as soon as Dawn number occupy over here, including increment one and now the consumer can come in. 100 00:13:11.490 --> 00:13:22.500 William Cheng: Right, because we assume that the consumer is running the other CPU is waiting to come in as soon as this y equal to what he will accommodate that command to zero, right, so then this go to zero right away. Okay, and now 101 00:13:22.950 --> 00:13:26.520 William Cheng: Next out over here, equal to zero. So, what it will do is it will take this data. 102 00:13:26.730 --> 00:13:35.280 William Cheng: And then copy that mean to item over here. So now this item disappear. I mean, the item doesn't really disappeared is still there. Right. But we consider that now it's no good because we have taken the data out. 103 00:13:35.610 --> 00:13:42.300 William Cheng: Okay, so I'm going to change it back to the empty color over here. The next I'll pointer over here. I'm going to change it to the next location right here. 104 00:13:42.570 --> 00:13:49.560 William Cheng: They do the wraparound check and then perform a be operation on the empty because now I have eight empty spots now. 105 00:13:49.890 --> 00:13:53.610 William Cheng: Okay, so now they will do that. I'm going to take a local variable we here. I'm going to return from 106 00:13:53.880 --> 00:14:04.740 William Cheng: There. So again, in this case, the local variable be here sit inside the stack rank. When I returned from it the function will return this value. And as soon as you return for this, the function. This data become invalid. 107 00:14:05.640 --> 00:14:11.880 William Cheng: Okay, so in this case this is okay because I'm not pointing into the stack frame. I'm actually copying all this data and then returned from 108 00:14:12.090 --> 00:14:24.330 William Cheng: The return on the function. So, therefore, I get a copy of the data already. Okay. So, therefore, you know, even though the old data sitting on the stack rank and now when you return for the function, the stack returning to garbage and then still everything's okay yeah 109 00:14:25.770 --> 00:14:37.020 William Cheng: All right. Alright, so now returned from here are the producer will continue to produce and the consumer can continue to consume anytime they are ready to to to to interact with each other. They're gonna call these functions. Okay. 110 00:14:38.610 --> 00:14:42.900 William Cheng: All right. So in this case, it seems like the producer consumer, they all work in the same spot. 111 00:14:43.200 --> 00:14:52.410 William Cheng: Okay, so therefore we're going to end up with no parallelism. Right. But what if the producer start running a little faster produces going to stop producing right here to consumers still taking all the data very, very slowly. 112 00:14:52.890 --> 00:14:56.130 William Cheng: Okay, so in that case, what happened is that the code in the middle we here in the 113 00:14:57.360 --> 00:15:05.490 William Cheng: Middle over there, they can actually run in parallel. Okay, so this code, I can actually have one thread running over here on the left hand side. And notice the running on the right hand side over here. 114 00:15:05.670 --> 00:15:14.130 William Cheng: And these two threads will be able to run in parallel to producer will keep putting putting data like right here, the consumer will take data out from another part of the buffer. 115 00:15:14.910 --> 00:15:21.030 William Cheng: Okay, so you can sort of invent. Imagine if the conveyor belt is pretty long. Well, then chances are, most of the time. You can do this in parallel. 116 00:15:21.660 --> 00:15:30.120 William Cheng: Okay, unless you know one of them catch up with the with the other thread. In that case, they will be working on the same spot. Otherwise, you have parallelism now. 117 00:15:32.040 --> 00:15:41.970 William Cheng: All right, so you know if the producer and the consumer the if they are working at a different spot and they consume at the same rate. Then, you know, no one will ever have to wait for another threat. 118 00:15:42.540 --> 00:15:49.950 William Cheng: Right, if they're working on different parts over here, we actually we're going to end up with the parallelism of to threats, right, we have two CPUs will be able to run in parallel out. Okay. 119 00:15:50.220 --> 00:15:59.220 William Cheng: If the producer is faster than the consumer is slow eventually produce a way if the consumer is fast and it produces slow eventually they're going to catch up with each other and the consumer will have to wait. 120 00:16:00.480 --> 00:16:10.170 William Cheng: Alright. So in this case, if you write your code. This way, you're going to have maximum amount of parallelism when they are working on different spot and when they just happen to be working in the same spot. Well then, then one has to wait for the other 121 00:16:11.220 --> 00:16:16.920 William Cheng: OK. So again, the problem with using only one new taxes that you can use when you try to lock up everything right. So this case. 122 00:16:17.130 --> 00:16:24.510 William Cheng: This Cohen this co they will be critical. And so if I replace the P and the V over here with piece of me text lock and piece of meat has unlock 123 00:16:25.050 --> 00:16:30.540 William Cheng: What, then, the code in between over here. They're all critical section called and then there will be absolutely no parallelism. 124 00:16:30.900 --> 00:16:44.250 William Cheng: Right, because they're critical section co with respect to the same new tags. So, therefore, this code and this code, they are mutually exclusive. Okay. By using a 74 you're going to end up having more pills. Okay. And that's the whole point of using a semi for here. Yeah. 125 00:16:46.440 --> 00:16:53.790 William Cheng: Alright so new test myself right so again I can lock everything up using one giant new tax right so we hear P therapy tax law PCBs unlock and lock and unlock 126 00:16:54.000 --> 00:17:00.900 William Cheng: All use exactly the same new tax. So this one provide coarse grained problem is and you basically have no problem at all. Okay. 127 00:17:01.860 --> 00:17:11.520 William Cheng: So, so, so, so that could be one way. So it's still, it's a correct way to implement this, but it's just not very, very efficient because most of the time you're working on different spot on the conveyor belt. 128 00:17:11.880 --> 00:17:19.800 William Cheng: Okay, Center for give you more fine grained parallelism, right, because in this case I have two thirds. I want to run in parallel. Most of the time I will able to do that. 129 00:17:20.430 --> 00:17:32.280 William Cheng: Okay, so therefore summer for gives you a fine grain Prilosec okay all right here there's a comment right here to say the summer for is actually not very general enough as it turns out, the only place that you might want to use the 130 00:17:35.190 --> 00:17:41.730 William Cheng: Is exactly on a producer consumer problem. Okay, if you want to do anything else you know semaphore is actually not very, very useful. 131 00:17:44.190 --> 00:17:53.040 William Cheng: Alright, so seven for has limited use right, pretty much the only place is really good for is producing consumer problem. Okay. Although this is a very important application or semi for 132 00:17:53.430 --> 00:18:03.960 William Cheng: Okay, so a lot of times when you try to write a write a program, what you will do is that you will use something called pipeline parallelism. I'll look like this. Right, this is your program. You have five threads 12345 over here. 133 00:18:04.470 --> 00:18:11.460 William Cheng: Throw one over here read data from the input on what it will do is that it will process the input and then send it to throw a tool for further processing. 134 00:18:11.820 --> 00:18:17.520 William Cheng: Okay, so I to over here will take the, the, the, this sort of the data that has been has been processed. 135 00:18:17.730 --> 00:18:22.530 William Cheng: Through the first stage. And then, you know, further processing data and then create data for the next stage. 136 00:18:22.710 --> 00:18:29.970 William Cheng: And three, three over here or pick up data over here and then it will work on it and then produces and for the fourth threat and then it will produce data for the fifth thread and I'll be here eventually 137 00:18:30.300 --> 00:18:34.980 William Cheng: Fifth will be the one that produce the final output. Yeah. So why would you want to do that. 138 00:18:35.730 --> 00:18:41.220 William Cheng: Okay. So in this case, you know, the way that you pass data up on one side to the to the other is to use over here. 139 00:18:41.580 --> 00:18:50.340 William Cheng: You know, the picture over here I draw them like acute right so you can include some data over here and the other I can DQ it, but typically this is done using a producer consumer problem. 140 00:18:50.580 --> 00:18:56.070 William Cheng: Okay, because the buffer that you use over here is not my forward to this right, am I for two lives can be infinitely long 141 00:18:56.460 --> 00:19:01.230 William Cheng: Okay, in this case, this queue right here, over here is going to be a Q with bounded buffer space. 142 00:19:01.830 --> 00:19:09.030 William Cheng: Okay, so now we're going to implement it using an array using a producer consumer problem. So, this way we can actually limit the amount of data that our program will use 143 00:19:09.480 --> 00:19:13.740 William Cheng: Okay, if you allow your program to have infinite amount of data or pretty soon gonna use up all the memory. 144 00:19:14.070 --> 00:19:20.040 William Cheng: Okay, so therefore it's actually not a great idea to have, you know, to, to have to have your data show to grow to emphasize 145 00:19:20.490 --> 00:19:29.100 William Cheng: Yeah, so a lot of times if you are writing a very serious program, whatever it may be, can make it run for a very long time. We're going to use the Bounder BA BA BA for it. We're going to use a producer consumer problem. 146 00:19:29.520 --> 00:19:35.760 William Cheng: Okay, so. So the advantage of doing something like this. Is that because all these five fields over here, they will run independently. 147 00:19:35.940 --> 00:19:46.110 William Cheng: They only synchronize when they have no work to do. Right. So when threat to over, he has no work to do what he will do is that it will come and wait for the producer consumer problem way for Tier one to produce work for it. 148 00:19:46.650 --> 00:19:53.640 William Cheng: Okay. Similarly, any threat over here. What they would do is that they will come away from the data. So when they come worry for the data they were voluntarily give up the CPU. 149 00:19:53.850 --> 00:20:04.350 William Cheng: And the way you write your code that you don't have to explicitly give out the CPU. All you need to do is to call the P, you know, the to the call the P function and that function will automatically make you give up the CPU. 150 00:20:05.430 --> 00:20:13.200 William Cheng: Okay. So in this case, again, if all these two are they all have work to do there will be able to work in parallel, if only one of the threads over years work to do that only one author will be working 151 00:20:14.220 --> 00:20:22.050 William Cheng: Okay, so this way you can write your graduate program. Very, very nicely. All these threads that are independent. They only synchronize once in a while, so therefore you can minimize the 152 00:20:22.380 --> 00:20:28.770 William Cheng: Minimize the bugs in your code and also you can have as much power as the mess you have when you actually have five thread. They all have work to do. 153 00:20:30.270 --> 00:20:33.870 William Cheng: OK. So again, this is a very, very important use of your 154 00:20:34.920 --> 00:20:49.110 William Cheng: important use of December for. And as it turns out, this is the only place I wasn't before is actually very useful. Okay. Most of the time we need something better than center for. Yeah. All right. So, number four is used to to write your code. This way to provide pipeline parallelism. 155 00:20:52.500 --> 00:21:00.780 William Cheng: Right said before is actually a very popular apologetics, you know, they not only provide you with new tech. They also implement this demo for from scratch that. 156 00:21:01.290 --> 00:21:08.670 William Cheng: So you can initialize itself before he can destroy December for you can wait for a seminar for. So this one is the same as a p operation is called a somebody different. 157 00:21:09.000 --> 00:21:18.930 William Cheng: And then there's a be operation, they call a center for post. Okay, so when you post a 74 that's atomic increment operation where you wait for some of what atomic detriment of the operation. 158 00:21:19.530 --> 00:21:29.430 William Cheng: That. So if you write your code like this. Okay, so I'll be here I have my producer consumer I use the P and V. So since I'm using P and V over here. This code is actually a pseudo code. 159 00:21:30.870 --> 00:21:39.630 William Cheng: Okay, the code. I just walked through over here. This is pseudo code. All right, but I can easily change it into sequel using positives library by converting P to Sam wait and 160 00:21:39.840 --> 00:21:51.750 William Cheng: by converting the two samples. So over here, it's going to be Sam wait anti Sam wave, you know, samples occupy. And then for the consumer over here. I'm going to call Sam way occupy and Sam oppose empty and this code now becomes the code. 161 00:21:53.190 --> 00:22:02.040 William Cheng: Okay, so that's one of the great thing about writing your code, you know, in the sort of the pseudo code of the garden command, it's very, very easy to convert them into sequel. 162 00:22:02.850 --> 00:22:09.180 William Cheng: Yeah, so, so, so again we're going to write our code. This way, I'm going to talk about our co using this higher level abstraction and uses have a four or 163 00:22:09.570 --> 00:22:18.120 William Cheng: Although and also other kind of a construct that we're gonna look at very, very quickly look at them very soon. And then we can easily cover them to seek. Oh, yeah. 164 00:22:20.010 --> 00:22:28.980 William Cheng: All right, so how do we actually implement the summer, for I saw so get the seven four is only P and V operation. So the question is, how do we implement a p operation. How do we implement the V operation. 165 00:22:29.250 --> 00:22:42.630 William Cheng: Okay, as it turns out we can implement and then using just a mute tax. Okay. So on the left hand side over here I have the summit for okay on the right hand side over here I have the P 30 implementation. OK, so I guess I 166 00:22:44.130 --> 00:22:53.550 William Cheng: Should correct myself on the left hand side over here, we have to guard a command for the P operation off for the V operation right here. Okay. On the right, we're going to see how to implement them using the pizza library. 167 00:22:54.720 --> 00:23:02.490 William Cheng: Okay, so, so I'm going to claim that the you know the right hand side, the left side over here. They're equivalent. Okay. Alright. So the way I'm going to do this is 168 00:23:02.850 --> 00:23:16.290 William Cheng: So we here says when x is greater than zero. So you could ask us as good as it minus one. OK. So again, the semantics over here is that if you see that the guard is true. You have to automatically beckerman at the detriment as by one 169 00:23:16.980 --> 00:23:25.050 William Cheng: Okay. So this guy's, how would you implement this, right. So I'm going to sort of stay inside a for loop, because if it turns out that the guard is false. I have to keep sustains at the simple to do forever. 170 00:23:25.350 --> 00:23:34.290 William Cheng: Okay, so therefore I nearly infinite loop, right. So the code over here. I'm going to start with the infinite loop. And the first thing that will do over here is that in order for me to synchronize I only have one way to synchronize is using your tax. 171 00:23:35.040 --> 00:23:46.320 William Cheng: Okay. So, therefore, what I will do is I would love to be taxed. And now I'm going to look at the guard over here. If the guard is true, what should I do. So if x is greater than zero. So if the guard is true. I will actually go to come and see us. 172 00:23:47.190 --> 00:23:54.990 William Cheng: Okay so command sequence over here is exactly as equal to x minus y. So if x is greater than zero. I'm going to execute is equal to x minus one. 173 00:23:55.230 --> 00:24:02.730 William Cheng: And then I'm done. Right. So, therefore I'm going to copy for me attacks unlock. I'm gonna call Bray to get out of the infinite loop. And now I returned from the P operation. 174 00:24:05.130 --> 00:24:09.630 William Cheng: Okay, and what it has is false. Okay, so what is FS is equal to zero. 175 00:24:09.990 --> 00:24:16.710 William Cheng: So the guard is false. If the guard is false. All I do is copy that means has unlocked. And that goes back into the into the infinite loop. 176 00:24:16.890 --> 00:24:27.420 William Cheng: Copies I mean tax law again and chances are, you know, the guard is still false. So, therefore, this is going to be go to zero and copies of music unlocked, and then I come back up here, over here, and then do this over and over and over again. 177 00:24:28.080 --> 00:24:37.440 William Cheng: Okay, so in that case. All I'm doing what we hear is that I locked the new tax see that God is false. I'm locked in your tags. I like to mutate see the goddess false. The alchemy attacks. 178 00:24:38.160 --> 00:24:48.840 William Cheng: Okay, so in that case, am I implementing this, you know, did this P operation. I am I am actually implement this P operation because if God is false. I don't do anything. I keep waiting for the guard to become true 179 00:24:50.250 --> 00:25:02.220 William Cheng: There. So if you look at the right hand side over here. What is the critical section over here, right, if the guard is false, the critical section is between PCB type lock me, Tom law. So this case is as equal to zero then this is my, you know, 180 00:25:02.850 --> 00:25:15.000 William Cheng: So, so, so, so, so again you need to look at this very carefully right I locked the new tax, I see that as equal to zero I unlocked me attacks. Okay. So inside the critical section. There's only one statement that's by seeing as equal to zero. 181 00:25:16.800 --> 00:25:26.040 William Cheng: Alright, so that would be one critical section. The other critical section is to see that S is greater than zero, right, if the guard is truly I see the essence greater than zero. I detriment as by one 182 00:25:26.280 --> 00:25:31.080 William Cheng: And then I performed piece of meat has unlocked. So this call right here is a critical section called 183 00:25:32.850 --> 00:25:37.710 William Cheng: Okay, so you can see that this call overall has to critical section Co. One is to see the guard is equal to 184 00:25:38.130 --> 00:25:44.640 William Cheng: The guard is true and the other one's got to go to false. Okay. When God he go to true. So this co implementing this semantics. 185 00:25:45.510 --> 00:25:50.520 William Cheng: Okay. So remember when I explained the garden command to you, right, if the guard is true when you see the guard to be true. 186 00:25:50.700 --> 00:25:58.230 William Cheng: You have to execute a command sequencing one atomic operation. So when I see the guard to be true as his word and zero I execute a command sequence. 187 00:25:58.470 --> 00:26:07.920 William Cheng: Did I do this in one atomic operation. Well, yes, because the code or execute over here is a critical section call and then this code is going to be an atomic operation with respect to the new tax. 188 00:26:09.030 --> 00:26:12.270 William Cheng: Okay. And is it true that if God is false. I don't do anything. 189 00:26:12.900 --> 00:26:19.530 William Cheng: Well, if the guard is false. Over here I see is equal to zero, then I call piece of meat, I love, I love to meet other unlock meters are locked me that 190 00:26:19.890 --> 00:26:32.310 William Cheng: I didn't really do anything. I didn't do anything useful, other than lock and unlock the new tax. Okay, so therefore the CO on the right over here as an exact implementation of the pseudo code for the P operational left 191 00:26:34.140 --> 00:26:41.700 William Cheng: Okay, so. So again, you need to read this call and then sort of draw timelines to convince yourself that this code actually implement this. If you have questions, feel free to send me email. 192 00:26:42.060 --> 00:26:51.300 William Cheng: That all right how do you implement the operation over here. Well, that's pretty simple. There's no guard so i. So, you call Peter new tax law as equal to x plus one and then piece of meat has unlocked. 193 00:26:51.480 --> 00:26:58.080 William Cheng: So is this an atomic operation. Well, yes. Right, because I locked the three machine structure together into one atomic operation. 194 00:26:58.620 --> 00:27:01.800 William Cheng: Okay, because the code is executed instead of critical section. Okay. 195 00:27:02.640 --> 00:27:12.450 William Cheng: All right. So in this case, all these three critical section. Are they are all mutually exclusive right i mean over here, you know, for the P operation. There are two critical section over here. There's another critical section right 196 00:27:15.120 --> 00:27:24.360 William Cheng: All right, so what is wrong with the implementation over here. Okay, so the implementation over here it's correct implementation write this code over your correctly implemented the others operation. Okay. 197 00:27:24.900 --> 00:27:32.550 William Cheng: But it turns out it's inefficient, right, because if you know what if the guard is false, right, if the guard is false and this guy's. That means that S is equal to zero, x is equal to zero here. 198 00:27:33.360 --> 00:27:40.890 William Cheng: Okay, so as you go to zero over here. We're gonna wait for another threat to change. So the increment is equal to one. Okay. What if that's why it's taking a long time. 199 00:27:41.850 --> 00:27:46.950 William Cheng: Okay, so in this case what would our threat do right are threat over here. We're locked in a taxi is equal to zero, unlocking new tax. 200 00:27:47.160 --> 00:27:53.850 William Cheng: Law to meet. I see is equal to zero. And I'll ask them your tags over here, right. So if the guard is false and no other thread is waiting for the meantime as 201 00:27:54.060 --> 00:27:59.880 William Cheng: Well, in that case whenever we call piece that new tax law. We're going to enter the serialization box right away without give up the CPU. 202 00:28:01.140 --> 00:28:07.440 William Cheng: Okay, so remember a piece of new tax law is the blocking call a blogging call that means that it is possible that it will get blocked. 203 00:28:08.130 --> 00:28:14.970 William Cheng: Okay, but if nobody's waiting for this new tax. Well, then, in this case, you won't get blocked, you will actually, you will not give up the CPU when you call piece of music. 204 00:28:15.540 --> 00:28:22.080 William Cheng: And then what you would do is that he was seen as equal to zero and you unlock them. In the meantime, when he allowed me to, you don't get blocked either. And then you come 205 00:28:22.290 --> 00:28:28.140 William Cheng: To me and I said, Yeah. And again, you won't get blocked. So therefore, you're going to be running this code forever. And then you're using the CPU. 206 00:28:28.560 --> 00:28:36.030 William Cheng: So if you're running your CPU on your cell phone, your cell phone is going to get really, really hot. And then the battery is going to go down pretty soon well put up a poor, poor pretty quickly. 207 00:28:36.690 --> 00:28:43.470 William Cheng: Okay, so this is no as Busy waiting your thread will not give up the CPU while waiting for the guard to become true 208 00:28:44.250 --> 00:28:50.430 William Cheng: Okay. So in this case, you know why it's got busy waiting right you're waiting for something you're spinning the CPU clock, but you are not doing anything useful. 209 00:28:50.910 --> 00:29:00.450 William Cheng: Okay, but he said one thing some useful. I'm waiting for the garden become true. Well, but you don't have to wait for it this way that the correct way to wait for something is to give up the CPU. 210 00:29:01.710 --> 00:29:04.740 William Cheng: Okay, you're waiting for the guard to become true, why are you burning up the CPU. 211 00:29:06.030 --> 00:29:14.700 William Cheng: Okay. So, so what if you only have one CPU. If you're printing out the CPU, then the other thread doesn't get to run right the other thread you know when it gets run, it will increment, you know the the seven for 212 00:29:15.000 --> 00:29:17.580 William Cheng: A while. But you don't let it run because you're burning out the CPU. 213 00:29:18.180 --> 00:29:29.880 William Cheng: Okay, so eventually you use up too much of the CPU the organisms gonna yank it out of the CPU and give the CP to somebody else. But then in this case, again, you have to waste you have wasted a lot of the CPU cycle. You also have wasted a lot of batteries. 214 00:29:30.810 --> 00:29:34.860 William Cheng: Okay, so even though this is a correct implementation, but it's not very 215 00:29:36.330 --> 00:29:47.490 William Cheng: Efficient okay so we got to learn how to write program efficiently. So if you want to wait for a guard to become true and again a guard it just a collection of variables you write some expressions for and evaluated, true or false. 216 00:29:48.630 --> 00:29:53.430 William Cheng: Okay, so. So in that case, if you if your guard is false, you know, then, then you need to go to sleep. 217 00:29:53.880 --> 00:30:00.750 William Cheng: Okay. And later on, when somebody changes the variable that will change the guard. Well, then you should be woken up and then you can get to evaluate the guard again. 218 00:30:01.680 --> 00:30:04.020 William Cheng: Okay. So that's the basic idea. Yeah. 219 00:30:04.590 --> 00:30:13.500 William Cheng: Alright, so we're going to talk about the implementation of the general garden command right so now the guard over here can be very, very complicated. We saw that in the summer for case guard is just as greater than zero. 220 00:30:14.250 --> 00:30:22.380 William Cheng: Right. It's very simple. I mean, also for the V operation. There's no there isn't no guard. Okay, so, so the sum of what is the sort of the simplest case for Garda command. 221 00:30:25.860 --> 00:30:30.480 William Cheng: So in general, at the example I use before, right, is, you know, you know, the guard can be this expression. 222 00:30:30.720 --> 00:30:44.670 William Cheng: A greater than three and FOB is less than equal to see okay and a BSc are three different variable being changed by three different threads in three different CPU simultaneously while you are the fourth threat and you try to evaluate discard. 223 00:30:45.990 --> 00:30:49.740 William Cheng: Whereas we saw the picture right the car will go up and down like crazy right somebody in this case again. 224 00:30:49.920 --> 00:30:58.590 William Cheng: How would you evaluate this guy, right, I mean, clearly you going to use a new tax when you lock them up as you're going to lock on all the other threads. OK, and now you come and try to evaluate this car. 225 00:30:59.550 --> 00:31:07.740 William Cheng: Okay, so, so if you do that, you can actually will be able to get a snapshot of the GAR. And if the guard is false. What, in that case, would you would do is that you unlock them he attacks and then 226 00:31:08.040 --> 00:31:14.010 William Cheng: Would you would do is you a lot to me attacking you try to evaluate the guard again and we're going to end up with exactly the same problem as before, we're going to end up with Busy waiting 227 00:31:14.670 --> 00:31:19.440 William Cheng: OK. So again, if you try to evaluate this expression, what you can do is that you can put ABC. 228 00:31:19.680 --> 00:31:29.880 William Cheng: All inside the serialization box. And when you try to evaluate this expression you do it using a critical section. And when all the other third when they try to modify a, b, and c. They also they will use a critical section. 229 00:31:30.630 --> 00:31:36.690 William Cheng: Yeah, so the picture look like this. Right. We know that we will talk about this here ization bug right every civilization box in 230 00:31:36.930 --> 00:31:40.980 William Cheng: every civilization box is part of a new tax right here's the new tax, here's the new tax queue. 231 00:31:41.220 --> 00:31:47.340 William Cheng: And then all the share variable, you're going to put it inside of summarization box to as sort of as a notation to yourself to say that 232 00:31:47.580 --> 00:31:54.270 William Cheng: Nobody's allowed to read or modify a, b, and c unless they are the thread that's executing instead of civilization box. 233 00:31:54.480 --> 00:32:01.470 William Cheng: Okay. And if you are the threat execute instead of supervision box. That means that you on the new tax right you copies me that's not a you return a piece of meat at all. 234 00:32:01.590 --> 00:32:10.170 William Cheng: So now you're executing critical section call. So when you execute critical section code you're going to examine ABC. You're gonna, you know, evaluate a guard you can you get to do all these kind of things. 235 00:32:10.920 --> 00:32:18.810 William Cheng: Okay, so, so everything is captured in this picture over here. So if you do something like this. If you are the four threads. You go add yourself to the meat has to be here. 236 00:32:18.990 --> 00:32:24.390 William Cheng: When you're executing the middle. Now you know for sure all the other third. They're not modifying a, b, and c 237 00:32:25.140 --> 00:32:32.190 William Cheng: OK, so now you can actually take your time to evaluate this expression, and if it turns out that the other you know the the guard over here is false. 238 00:32:32.370 --> 00:32:42.660 William Cheng: What would you do you come back over here to the meat AX queue. And if nobody else is sitting in the meat ax, you know, you're going to do this over and over again, right, because next time when you come into this realization box. What should be the value of the guard. 239 00:32:43.710 --> 00:32:54.360 William Cheng: What it is, you know, because the rule over here is that if any other threat if they want to change. A, B, or C. They also have to come to this realization box right they have to lock them your task and come inside to modify a, b, and c 240 00:32:54.840 --> 00:33:01.410 William Cheng: OK, so now the assumption over here is that all the other said they are not changing. A, B and C. So in this case, you're going to end up during this Busy waiting 241 00:33:01.680 --> 00:33:04.080 William Cheng: You know, because the value of A, B, and C doesn't change. 242 00:33:04.320 --> 00:33:11.550 William Cheng: Is the value of A, B, and C does not change. Well, then the value of the guard is going to continue to stay false right because last time when you evaluate the garden was false. 243 00:33:11.790 --> 00:33:20.520 William Cheng: And now a BSc stay exactly the same while. So therefore, the goddess of and be false. Okay. So will you start doing this over here, you're gonna have a busy waiting. So again, that's going to be really bad. 244 00:33:21.540 --> 00:33:29.130 William Cheng: Yeah, alright. So, what is going to be the solution, right, the solution over here is that, you know, if we only use a new tax that's not enough, we have to use something else. 245 00:33:29.610 --> 00:33:40.590 William Cheng: OK, so the positives library, give us an additional perimeter for you to deal with a situation like this right deposit elaborate give us something else, so that we can implement a general garden command correctly. 246 00:33:40.890 --> 00:33:47.490 William Cheng: Okay, so again correctly, meaning that will you try to wait for something you need to go to sleep and wait for it. Okay, and don't do Busy waiting. Okay. 247 00:33:49.440 --> 00:33:55.080 William Cheng: Alright, so if there's something else that we that we're going to use is known as the condition variable. It's kind of a strange name. 248 00:33:55.410 --> 00:34:00.540 William Cheng: It's a variable. But it's used for condition. There was a. So what does that mean, right. 249 00:34:00.720 --> 00:34:09.720 William Cheng: So what's important also for us to understand is that when we start using the new variable. We have to follow a bunch of rules okay if you don't follow this rule, then there's no guarantee that they will work. Right. So again, 250 00:34:09.930 --> 00:34:19.200 William Cheng: You know, just like the operating system. The Peter library, give you a bunch of primitive give you a bunch of functions to say that this is the right way to use it if you don't follow the rules, then all bets are off. 251 00:34:20.100 --> 00:34:26.490 William Cheng: Okay, so you have to learn what the rules are. So this way when you use them, they will perform, they will do things the right way. Okay. 252 00:34:28.890 --> 00:34:33.900 William Cheng: Okay, so let's talk about the politics condition variable, also known as a CV right condition variable over here. 253 00:34:34.440 --> 00:34:42.360 William Cheng: They are used to implement the most general form of the Garda command. OK. So again, Dykstra invented of the sort of the, the garden command. 254 00:34:42.600 --> 00:34:49.740 William Cheng: As it turns out, the Garda command is very, very general. Okay, they can they can be used to implement any kind of a synchronization that you want. 255 00:34:50.250 --> 00:34:56.940 William Cheng: Okay, summer for against very limited. If you use mutates by themselves. Well, then in that case is going to be inefficient. 256 00:34:57.480 --> 00:35:01.800 William Cheng: Okay, so therefore, again, we're gonna we're gonna add another thing, and they're called condition variable. So using 257 00:35:02.190 --> 00:35:08.250 William Cheng: You know the mutation condition variable together you can implement any garden command efficiently. Okay. 258 00:35:09.030 --> 00:35:13.020 William Cheng: Alright so here we're going to sort of draw this picture over here. So, again, from the previous picture over here, right. 259 00:35:13.470 --> 00:35:18.480 William Cheng: Sorry, from the previous picture over here is like this. Right, we're going to add another thing over here called the condition variable. 260 00:35:18.720 --> 00:35:30.600 William Cheng: There so it will look like this. Here's a condition variable we here with joy inside the civilization box to remind you that you are not allowed to touch the condition variable unless you are the threat. That's actually inside 261 00:35:31.500 --> 00:35:46.470 William Cheng: OK, so now we will, you will you own the new tax. What can you do right you you can execute a you can actually have critical section call when you actually critical section Co. You can look at ABC can modify ABC or you can do something with a condition where ball. 262 00:35:48.180 --> 00:35:55.950 William Cheng: Again, so what is the condition variable, right, the condition variable over here is a queue of threats. Okay so conditioning variable is my for to list. 263 00:35:57.750 --> 00:36:00.060 William Cheng: Okay, the condition variable over here is just the list. 264 00:36:00.600 --> 00:36:08.220 William Cheng: Okay, so here's a cute. So when you think about a curious as first in first out right but again it doesn't really have to be first in first out. It depends on the actual implementation. 265 00:36:08.460 --> 00:36:13.320 William Cheng: Okay, so all these also it's a cure threat. So what happens. Are you gonna have a bunch of threat sleeping over here. 266 00:36:13.800 --> 00:36:21.240 William Cheng: Okay, so again, think about my forward to list right inside the you know the my fortuitous element, you're going to point to a threat identify, are you going to point to a threat control bar. 267 00:36:22.020 --> 00:36:28.920 William Cheng: Okay, so a bunch of third over here. They are sleeping inside the condition verbal cue, they're sleeping there to wait for a notification. 268 00:36:29.310 --> 00:36:34.740 William Cheng: Okay, so what's a notification right you have cell phone, right, you get notification. Right. So you're doing something and you hear your phone golf. 269 00:36:34.950 --> 00:36:39.750 William Cheng: That's a notification. Right. And you can look at your phone and then you need to decide what would you want to do that. 270 00:36:40.200 --> 00:36:52.380 William Cheng: But, but sort of the way we sort of think about notification over here is not the not exactly the same as the phone notification, the kind of notification though that we're talking about over here. It's like the beep that goes off when you receive a notification. 271 00:36:53.670 --> 00:36:58.260 William Cheng: OK, so the people that goes up over here. If you don't hear it, whether you don't know that you'll be notified. 272 00:36:58.710 --> 00:37:07.320 William Cheng: Okay, so this is also known as the event. Right. The event is the, you know, sort of the phone, you know, make a little sound. Okay, so there will be an event and that also is known as a condition 273 00:37:09.000 --> 00:37:15.810 William Cheng: Okay, so that's why this is called condition variable over here because your thread is sitting over here waiting for some condition to become true 274 00:37:16.590 --> 00:37:20.460 William Cheng: Okay, waiting for an event to happen, waiting for a notification to be sent to you. 275 00:37:21.060 --> 00:37:24.480 William Cheng: Okay, so in this case thread waiting for a guard to become true 276 00:37:24.750 --> 00:37:34.800 William Cheng: Okay, so if you are, where you come in. Evaluate the garden. God is false, then what would you do right if you get moved back to the mute sq over here and got and I was busy way. So in this case, you have to wait at a different place. 277 00:37:35.400 --> 00:37:46.350 William Cheng: Okay, so what do you would do is that you will add yourself to the condition variable queue and then you will wait there, right. So again, the way we think about is that you call my for two lists a pen, etc. As it turns out, condition bro bro is a little more complicated than that. 278 00:37:47.490 --> 00:37:55.890 William Cheng: Okay, but the basic idea over here is that when a threat evaluate the guard guard is false it needs to go to sleep. In this case, it needs to sleep inside this cube. 279 00:37:56.730 --> 00:38:07.530 William Cheng: That. So in this class, you're going to see you're going to hear this terminology over and over again. When a threat goes to sleep. It will sleep inside the queue or something, sometimes will say this. The third was sleep on the cube. 280 00:38:08.490 --> 00:38:19.620 William Cheng: OK. So again, think about a cube here. The third. The third one is asleep on top of it or sleep in. It doesn't really matter how you call that. Okay. Whenever a threat goes to sleep is sleep in a queue or asleep on the cube. 281 00:38:20.970 --> 00:38:32.820 William Cheng: Okay, so they don't we talk about the colonel going to use this term over and over again. Yeah. Alright. So when the threat needs to wait for the next chance to evaluate the GAR what he would do is that it will add itself to the condition variable Q and it will fall asleep. There 282 00:38:33.870 --> 00:38:43.950 William Cheng: Okay, so when you fall asleep and nobody wakes you up you sleep there forever. So again, in this case, you will, we will go to sleep inside. Inside the condition variable. Q. It is possible that you will sleep there forever. 283 00:38:44.460 --> 00:38:52.860 William Cheng: Okay. So in this case, you're waiting for a specific condition to be signaled Raza visitors or somebody said hey you know they throw you know, throw something I just say, Hey, wake up. 284 00:38:53.610 --> 00:39:06.930 William Cheng: So you're waiting to be signal to say, go ahead and and then and then in this case you will go ahead and to reevaluate the guard. Okay, so. So in this case, what are you waiting for, right, you're waiting for the right time to reevaluate the guard. Okay, so what is the right time. 285 00:39:07.980 --> 00:39:16.530 William Cheng: OK. So again, what happened over here is that you come to me. Declutter this a little bit right you're executing code over here you evaluate the guard guard is false. 286 00:39:17.160 --> 00:39:29.610 William Cheng: Okay. The Guard has fallen so you add yourself to the to the condition variable Q over here. When will be the right time to evaluate the guard again. Okay, if A, B and C. They don't change. Well, they should continue to sleep inside condition variable. Q. 287 00:39:31.770 --> 00:39:38.190 William Cheng: Okay. So if A, B, and C stay exactly where they are what, then there's no bother to evaluate guard because the call was still be false. 288 00:39:39.060 --> 00:39:46.170 William Cheng: Okay, so you only need to bother to evaluate the GAR when somebody else change. A, B and C. Right. I mean, you're not changing. A, B and C. 289 00:39:46.410 --> 00:39:57.180 William Cheng: Another search engine. A, B and C. So when another thread to execute inside of civilization box when they change a or when they change up or when they change. See, it's the job of the other thread to wake you up. 290 00:39:58.290 --> 00:39:59.700 William Cheng: Okay, so how did the wake you up. 291 00:40:00.750 --> 00:40:05.970 William Cheng: Okay, so you are sleeping, the kidneys verbal cue if nobody wakes you up, you're going to see Miss that IQ forever so often is that 292 00:40:06.120 --> 00:40:13.950 William Cheng: When they try to wake you up. They only have to do one thing they need to pick you up from the condition variable queue. And then they need to move it into the meat of the move you into the matrix here. 293 00:40:15.060 --> 00:40:27.240 William Cheng: Okay. Why don't have to move into your meat AX you what because you know this other thread that just change. A, B and C. That will be the thread eschewed incarceration box. So when they wake you up, they can really put you inside of supervision, but because somebody else is there already. 294 00:40:28.590 --> 00:40:36.900 William Cheng: Okay, so this one over here. What it will do is that it will pick you up from the condition where I will kill your sleeping right they will pick you up and they will move it to another to another sofa. 295 00:40:37.470 --> 00:40:40.530 William Cheng: OK, so moving on one server to another server. And now you're in the mute execute 296 00:40:41.370 --> 00:40:48.300 William Cheng: That so when he actually going to get woken up what eventually when this thread give out the CPU next word is going to come in with an extra give up a CP a time 297 00:40:48.540 --> 00:41:00.600 William Cheng: When this way, unlocking new tax. The next day will come in and then the next minute it will come in and eventually when when you know when the thread in front of you. I'll give you tax on that, guys, next time you're going to go into the search box and now you're going to get woken up 298 00:41:01.680 --> 00:41:03.600 William Cheng: Okay, so when you wake up. What are you supposed to do. 299 00:41:04.020 --> 00:41:11.010 William Cheng: What you're supposed to reevaluate the guard right because it because what happens when you wake up, it is possible. Another sweat will change the value of A, B, and C again. 300 00:41:11.220 --> 00:41:18.630 William Cheng: So maybe at the time, will you get moved from the condition variable cue to the meat ax, you maybe the guard was true, but they're all these other thread in front of you. 301 00:41:19.470 --> 00:41:23.700 William Cheng: Okay, all the other thread, they will change the value of A, B and C. So by the time you come inside the box over here. 302 00:41:23.910 --> 00:41:36.450 William Cheng: The first thing you should do is to evaluate guard. If the guard is true, then what do you do you execute a command sequence right one atomic operation. Okay. If the guard is false. What do you do you add yourself as an admission available to you go, you go you fall asleep again. 303 00:41:37.560 --> 00:41:40.020 William Cheng: Okay, so in this case it is possible. You go into the 304 00:41:40.260 --> 00:41:46.440 William Cheng: verbal cue and then somebody moving into the meat AX Q over here. And then when you get into the so ization box. The guard is false. 305 00:41:46.620 --> 00:41:55.020 William Cheng: You go to sleep in the condition wearable queue. And then next time somebody wake you up moving into the meat I to you can actually go through this over and over and over again but now is this Busy waiting 306 00:41:56.190 --> 00:42:11.220 William Cheng: Well, this is no longer Busy waiting right because, you know, you can actually give up the CPU and the ego sleeping. Second is variable cute. Okay. So in this way, you are not consuming power on your on your cell phone. You're not draining the battery you also now you are wasting CPU cycles. 307 00:42:12.420 --> 00:42:16.500 William Cheng: Okay. So, therefore, this is the preferred way to do it when you try to implement a guard command now. 308 00:42:18.840 --> 00:42:29.460 William Cheng: Alright, so here's a note. Right. Unlike notification or your cell phone right when you get a notification is going to stay on your phone for a long time. Okay, so the our, our, kind of notification. They happen at an instant of time. 309 00:42:30.090 --> 00:42:37.860 William Cheng: Okay, so if you are not waiting for, you know, if you're not waiting for this event, you're not waiting for the notification at the right time, you're going to miss the notification. 310 00:42:38.340 --> 00:42:45.900 William Cheng: Right. So for example you a notification is a bell going off if you are, you know, in a different room you're in somewhere else. You don't hear the bell. Why not because you're going to miss the notification. 311 00:42:46.230 --> 00:42:55.950 William Cheng: Okay, if you miss the notification if you miss the chance of moving yourself if you missed the chance of getting moved into the new tax Q, you might end up in the condition variable Q forever. 312 00:42:56.940 --> 00:43:00.510 William Cheng: Right. The reason is that what if that notification is a once in a lifetime event. 313 00:43:01.110 --> 00:43:07.500 William Cheng: Okay, if that only happened once. And if you if you miss that one that case you will get stuck inside the condition variable Q over here forever. 314 00:43:08.190 --> 00:43:13.230 William Cheng: Okay, so we're going to make sure that we will write your code to make sure that bad things never happen. Okay. 315 00:43:14.040 --> 00:43:20.310 William Cheng: Alright so here it says if you are now waiting for it. Okay then, then, then, then there's a possibility that you will you will miss it. 316 00:43:20.730 --> 00:43:31.470 William Cheng: How do you make sure that you don't miss a particular event or notification. Well, you have to follow all the rules, otherwise you will miss the event again. So again, it's very, very important to understand what all the rules are okay. 317 00:43:33.360 --> 00:43:45.840 William Cheng: All right, so, so, so you have a thirst, you know, sleeping, is that it's going to verbal cue over here and then another threat. When the threat executing inside of civilization box over here, they can actually change the change the value of the guard. 318 00:43:46.260 --> 00:43:54.630 William Cheng: Okay, how do you change the value of the guard. Right. So in our example, A, B, and C are variables that you can change the way you change a, b, and c, you're gonna end up changing the value of the dark 319 00:43:55.590 --> 00:44:00.720 William Cheng: Okay. So, therefore, what you can do is that this sweat when it changes the A, B, and C value of the GAR 320 00:44:01.050 --> 00:44:10.920 William Cheng: By using a critical section code over here. One thing that they should also do is that once they change ABC. They should wake you up, right, because you know maybe you know your guard is true. Now guys, so therefore they 321 00:44:11.610 --> 00:44:18.990 William Cheng: Didn't need to wake you up that it's turns on a piece of the library. There are two ways to wake up threats that are sleeping inside condition verbal cue. 322 00:44:19.290 --> 00:44:27.540 William Cheng: Okay. One of them is called signal when you signal a condition, you're going to pick up one of the threat inside the condition verbal cue over here and you move it into a meat ax, you 323 00:44:28.440 --> 00:44:35.970 William Cheng: Get which to it. You pick up while again this is implementation dependent right if you have a first in first out Q, then you pick your pick your, your 324 00:44:36.270 --> 00:44:48.120 William Cheng: Your take the queue that you pick up the threat and the head of the queue. You have a last in first out. Q You will pick up one at the at the tail. You can also use randomized algorithm, you can do whatever you want. Okay, you have party. 325 00:44:48.450 --> 00:44:57.360 William Cheng: The lots of lots of implementation with this particular queue. So the correct answer, you know, is that when you pick up a threat to move it into a meat AX you we don't know which one is going to get picked up. 326 00:44:58.470 --> 00:45:06.060 William Cheng: Okay, so this is called signal condition. If you're the thread actually inside over here. You can pick up a thread moving to meet you. By signaling the condition 327 00:45:06.870 --> 00:45:15.180 William Cheng: Okay. You can also broadcast the condition. So will you brokers, the condition, you're going to take all the threads inside the condition variable queue and move them all into the matrix here. 328 00:45:16.590 --> 00:45:22.350 William Cheng: OK. So, again, in this case, you know how to implement that already. Right. Alright, so when you signal to condition. What, what is that correspond to 329 00:45:22.620 --> 00:45:27.810 William Cheng: Right. It's like my for to list on link and then followed by my for two lists a pen and that's all of this. 330 00:45:28.470 --> 00:45:39.390 William Cheng: Okay, if you want to broadcast a condition over here, you stay in the loop right and then what you do is that you go through every element over here, you call only only only Arlene, and then you got a pen apparent apparent apparent until this list is completely empty. 331 00:45:40.830 --> 00:45:47.010 William Cheng: OK. So again, the conditioning variable over here is simply a queue of thread. So it just like my forward to this. OK. 332 00:45:47.850 --> 00:46:00.660 William Cheng: So again, when you're thirsty are actually reinserted civilization box over here, there is no guarantee that the girl will be true. So therefore, the first thing you come inside our civilization back over here. The first thing you should do is to reevaluate the guard. Yeah. 333 00:46:04.020 --> 00:46:16.020 William Cheng: Alright, so I think this is sort of a good time to break us. And the next thing we're going to do that we're going to look at all these pieces of functions, you know, for manipulating the the condition variable to okay or I'm going to take a brief break