WEBVTT 1 00:00:01.350 --> 00:00:10.559 William Cheng: Okay, this is the second part of lecture five. So let's take a look at how to manipulate a condition variable. So, 2 00:00:11.219 --> 00:00:15.630 William Cheng: You know, the, the first function in Peter library is called Peter a condition. Wait. 3 00:00:16.379 --> 00:00:26.220 William Cheng: So this is the function that you will use to go to sleep, you know, inside the condition variable here. Okay, so therefore get the requirement over here is that your thread has to be the one that's executing 4 00:00:26.700 --> 00:00:31.230 William Cheng: You know, inside the, you know, instead of civilization box over here. So this is going to be your thread. 5 00:00:31.440 --> 00:00:35.820 William Cheng: And your throat will copy through a condition way right because this one takes an argument of the conditioning variable. 6 00:00:36.000 --> 00:00:43.440 William Cheng: You are not allowed to touch the condition variable unless you are the thread that's executing inside of civilization box unless you are the thread that has the new tax law. 7 00:00:43.830 --> 00:00:50.640 William Cheng: Okay, so therefore the requirement will be yours at once you're inside over here you can copy their condition way. So again, the goal over here is that you 8 00:00:51.510 --> 00:00:57.780 William Cheng: As soon as you come into this realization box you check if the guard is true if the guard is false. You need to go to sleep in the condition variable cute. 9 00:00:58.170 --> 00:01:07.770 William Cheng: Guy, so therefore you will call Peter condition way which takes to argument. It tells you which condition variable kill you going to sleep in and also when you need to get woken up well which muta excuse 10 00:01:08.280 --> 00:01:14.490 William Cheng: Me to execute you get put put back on. Okay, so they have the idea here is that is for you to go here and then later on, come to the mute at you. 11 00:01:15.000 --> 00:01:19.740 William Cheng: Know guy. So in this case, you have to specify both arguments what condition rebel is and also what your taxes. 12 00:01:20.370 --> 00:01:27.360 William Cheng: That. So again, you must only call this function. If you have the new tax law, which means that you are the threats executing sexualisation box. 13 00:01:27.720 --> 00:01:33.030 William Cheng: And the semantics of this piece rare condition ways, a little more complicated. Okay. It's an atomic operation. 14 00:01:33.540 --> 00:01:44.670 William Cheng: So in this class every time when we talk about atomic operation, you got to be very, very clear and it's it's going to be very, very important, because without atomic operation, you know, operating system cannot be built the 15 00:01:45.360 --> 00:01:52.950 William Cheng: Library cannot be built. So the you know the atomic operation over here is very important. You need to understand exactly what I've been locked together. 16 00:01:53.430 --> 00:01:59.970 William Cheng: Okay. All right. So we call Peter condition way. This one says is a comically unlocked me attacks and wait for The Event. 17 00:02:00.720 --> 00:02:06.990 William Cheng: Okay, so they're actually two things happening. They are locked together in one atomic operation. So you're the threat is executing is that issue ization box. 18 00:02:07.140 --> 00:02:17.880 William Cheng: Right. The first thing in order for you to come outside. Right. So I saw this picture the cues outside over here. So you have to come outside of civilization bark bark box by unlocking the new tax guy. So first you unlock the metallics 19 00:02:18.120 --> 00:02:22.110 William Cheng: And then you go sleep inside the queue to wait for the event with a notification. 20 00:02:23.730 --> 00:02:32.640 William Cheng: Okay, so will you call piece or a condition way. Right. You need to understand this is an atomic operation anatomically unlock the new tags and then your threats are still going to sleep. 21 00:02:33.210 --> 00:02:37.530 William Cheng: Okay, and you sleep instead of engine verbal cue. Okay. And while you sleep over here, you're outside of the 22 00:02:37.950 --> 00:02:45.120 William Cheng: Box. So when you come out over here, you know, one thing that you can do is you can bring in another thread from the meters cube over here internationalisation box. So that's it will start running 23 00:02:45.720 --> 00:02:51.330 William Cheng: Okay but. But before you do before you can do that, you need to come outside over here and then eventually going to sleep in the queue. Okay. 24 00:02:51.810 --> 00:03:04.950 William Cheng: All right, eventually we got woken up right so so once you once you start sleeping instead of condition variable Q later on another thread is going to signal or broadcast condition they will move you into the new types Q without you executing a piece of code. 25 00:03:06.030 --> 00:03:11.520 William Cheng: Okay, so once you once you fall asleep, you know, instead of condition BBQ when we fall asleep. We don't execute code anymore. 26 00:03:12.120 --> 00:03:18.270 William Cheng: Okay, so, so it is the responsibility of have another thread to pick you up and then move it into your into the meat excuse 27 00:03:18.810 --> 00:03:25.170 William Cheng: Okay, when you're sitting in the mute mute. I still, you're still sleeping you're moving forward, as you know, people start getting into the serialization box. 28 00:03:25.440 --> 00:03:33.360 William Cheng: At some point, what is your chance. What are you going to do that, you're gonna you're gonna get moved into this organization box and then when you wake up, you're going to be, you're going to be running inside this irritation box. 29 00:03:34.170 --> 00:03:42.420 William Cheng: There. So therefore, when Peter a condition where return. What does that mean when this function returns. That means that you are back into the civilization box and you have the new tax law. 30 00:03:44.400 --> 00:03:55.800 William Cheng: OK. So again, where your thread called this function, right, the idea of this function is that you unlock the mutation, you go to sleep and one atomic operation and later on when you wake up when you return for this function, you still have the new tax law. 31 00:03:56.640 --> 00:04:01.170 William Cheng: Okay, so one way to sort of think about is that, you know, the fact that you own the new tax is 32 00:04:01.500 --> 00:04:09.420 William Cheng: The context of your threat, so, so, so what you do is that when you call this function, you will say the contact you own the meatheads and when you wake up, you still own dummy tax. 33 00:04:10.380 --> 00:04:19.320 William Cheng: Okay, in the middle of this function. In the meantime, just given to somebody else, right, because when you call this function you unlimited you go to sleep and then eventually when somebody wake you up with this function return you still on the meat ax. 34 00:04:19.980 --> 00:04:30.780 William Cheng: Okay. Very, very important to understand that when you call Peter condition where you have to make sure you have meetings lock so this way you save the contacts and when you return, you still have to do talks about you restore the contacts. Okay. 35 00:04:31.590 --> 00:04:35.940 William Cheng: Alright, so again this is a little convoluted, but you need to understand this is what's going on. 36 00:04:36.240 --> 00:04:42.870 William Cheng: There so so if I draw the picture right automatically unlocked me tags and wait for The Event. Right. So over here, Robert, is that what I call this function. 37 00:04:43.110 --> 00:04:55.560 William Cheng: Right before I have to, I have to have the new tax law. And then what I can do is I unlock the new tax and then wait for The Event and he want atomic operation because the tooth. These two things is considered to be to be happening at exactly the same time. 38 00:04:57.060 --> 00:05:03.840 William Cheng: OK. So, again, very, very important to to understand this. And again, it's a Tommy with respect to what why. So over here, there's a new tax involved. 39 00:05:04.260 --> 00:05:14.700 William Cheng: So, you know, because, because we have a civilization box right every civilization back has me tag associated to it were associated with that. Okay. So, therefore, a comically is with respect to the operational tasks. 40 00:05:16.230 --> 00:05:25.290 William Cheng: OK. So, again, very important picture to understand and then you must have the meat AX lock before you copies or condition. Wait, and then you unlock me tags wait for The Event one atomic operation. Yeah. 41 00:05:25.920 --> 00:05:32.370 William Cheng: Alright, so if you're waiting. Yeah, so, so, so, so, Robin. Is that another threat. Another student can be signal broadcasting your condition. 42 00:05:32.880 --> 00:05:39.150 William Cheng: Okay. So when can another threat signal Brock has the condition, given that piece to a condition way is at harmony cooperation. 43 00:05:39.630 --> 00:05:50.100 William Cheng: I mean, clearly, another threat cannot signal broadcast countries in the middle, right, because it's an atomic operation. So therefore, there's no way for another threat to signal Brock has a condition in the middle. Bill atomic operation. 44 00:05:50.550 --> 00:05:57.390 William Cheng: Okay, so the question is that kind of a kind of another threat signal broadcast condition before you perform this atomic operation. 45 00:05:58.290 --> 00:06:09.060 William Cheng: Okay, the answer will be no if you follow all the rules, right, because the rules is that, you know, right before you compete copies are kind of the way you are the one that on the new tag. So therefore, nobody else can be owning the new tax. 46 00:06:09.540 --> 00:06:18.900 William Cheng: Okay. And for all the other threat when they try to signal bras cuts the condition right if they tried to signal broadcast condition. They have to be inside of civilization. But that means that they have to own the meat ax. 47 00:06:19.590 --> 00:06:26.070 William Cheng: Okay, so therefore, for another 30 become impossible to signal Brock has condition right before the atomic operation. 48 00:06:26.940 --> 00:06:32.700 William Cheng: Okay, so if you cannot single broadcast the you know the condition before the atomic operation or in the middle of the 49 00:06:33.180 --> 00:06:41.610 William Cheng: Night or in the middle of the atomic operation. Well, therefore the only time you can signal Brock has a condition will be after you know it will be after the atomic operation. 50 00:06:42.630 --> 00:06:52.350 William Cheng: Okay, so, so therefore this is the only time that will be okay for another threat of signal broadcast going to condition if everything is implemented correctly. So in this case, are you going to miss. Are you going to miss this event. 51 00:06:53.400 --> 00:06:57.060 William Cheng: Well, there's no way for you to miss the event because you are already waiting for the event. 52 00:06:58.200 --> 00:07:07.050 William Cheng: Right, because once you know as soon as you finish calling this you know Peter condition where over here. Now you're waiting for the event. So therefore, the event happened afterwards. There's no way for you to miss it. 53 00:07:08.130 --> 00:07:11.700 William Cheng: Okay, so this is why we need to follow all the rules. If you follow all the rules and they will be 54 00:07:12.510 --> 00:07:18.300 William Cheng: then then then it will be, it should be clear that there's no way for you to miss the event, even though this event is a once in a lifetime event. 55 00:07:19.140 --> 00:07:24.900 William Cheng: Okay, save all the third follow all the rules, then this will work. Okay, if one of the third doesn't follow the rule. I gotta be in trouble. 56 00:07:25.620 --> 00:07:33.810 William Cheng: Okay. All right. So how do you signal Brock has condition. So, this guy's is very, very simple, right, you have a function called peace to a condition broadcast and you give the condition variable. 57 00:07:33.960 --> 00:07:39.540 William Cheng: OK. So again, instead of condition variable when he copies of condition way he remember which military is associated with 58 00:07:39.720 --> 00:07:49.080 William Cheng: So when you copy the condition broadcast with the condition variable over here. We're going to take all the threat in the condition verbal cue, we will simply going to pick them up and move them into the meat ax, you 59 00:07:49.620 --> 00:08:01.770 William Cheng: Okay, so all the stress, they are not executing any code at all the thread that's executing inside Association boxes to other copies to a condition broadcast. That's the one that doing all the job on link all these threads and then append them autonomy to 60 00:08:03.330 --> 00:08:08.160 William Cheng: Okay. The other function over here is called piece or a condition signal right again the argument over here is a condition variable. 61 00:08:08.280 --> 00:08:13.890 William Cheng: So, therefore, what it will do is that it will pick out one of the thread inside the condition variable Q over here and then move into the meat execute 62 00:08:14.760 --> 00:08:19.470 William Cheng: Well, so again, it's the same thing. That's why doesn't, you know, the third that sleeping doesn't get the executing curl. 63 00:08:19.650 --> 00:08:29.580 William Cheng: The thread that's inside of civilization box that the one that's calling Peter condition signal that's the one that doing all the job. It will link once way which threshold, you should you think the answer is, I don't know. 64 00:08:30.450 --> 00:08:35.760 William Cheng: Okay, it will pick out one of the one of the threads over here instead of Siddiqi over there and then move it into the mute execute 65 00:08:36.630 --> 00:08:46.590 William Cheng: Yeah, right. So you must only call all these three functions. Right. Peter condition way Peter condition broadcast and Peter condition signal if you have the corresponding new tax law. 66 00:08:47.910 --> 00:08:51.030 William Cheng: Okay, so again, the way we should just think about is that you can only call all these 67 00:08:51.180 --> 00:09:00.300 William Cheng: You know, these are the three function that deal with the condition variable. So you have to be inside a civilization box before you can you can call any function that's associated a condition variable, right. 68 00:09:00.480 --> 00:09:12.330 William Cheng: That's where I draw the picture. This way I put the conditioning variable condition variable inside the yellow box to be inserted civilization box to signify that you know only the threat that that has a matrix lock will be able to do anything to the condition variable. 69 00:09:13.980 --> 00:09:22.050 William Cheng: Right. Okay. So, you know. So again, you know, we hear this arrows over here is where you can signal Brock has condition. If you follow this rule. 70 00:09:22.650 --> 00:09:29.850 William Cheng: You know if another thread over here copies or condition way. Well, in that case, when you signal process to the condition that means that the other thread has to be waiting for 71 00:09:30.720 --> 00:09:38.610 William Cheng: The idea. The other thing is already sitting there waiting for the event to happen. Okay. So, therefore, there's no way for the other 30 missed event that 72 00:09:39.840 --> 00:09:46.980 William Cheng: Is over here. So as a kind of a silly question. Right. What if a piece or a condition. Wait. What did Peter condition way is not atomic 73 00:09:47.580 --> 00:09:55.890 William Cheng: Okay, what kind of thing can happen, right. So if all the way they all follow the rules. Okay. But there's a bug in the pizza, as I recall this operation over here is not atomic 74 00:09:56.070 --> 00:10:00.810 William Cheng: The, it is possible when somebody signal broadcasts condition you can may actually happen right right between them. 75 00:10:01.560 --> 00:10:07.590 William Cheng: Okay, so if you signal Brock has them vision right between that, then you know the thread has already unlocked the new tax. 76 00:10:07.980 --> 00:10:14.790 William Cheng: OK. And then as soon as he allowed me to fix the other three I can quickly locked him he attacks and then he was signal and broadcast or Brock has the condition 77 00:10:15.000 --> 00:10:22.560 William Cheng: And now the original thread over here that allows me to is it will wait for The Event. Well, but by that time the event has happened already. So therefore, in that case, you will miss it. 78 00:10:23.820 --> 00:10:35.160 William Cheng: Okay, so you can see that all these things has to work together and also piece to a condition where it has to be implemented as an atomic operation because if it doesn't, it is possible when you're unlucky, you're going to miss this particular event. 79 00:10:36.330 --> 00:10:46.230 William Cheng: Okay. So in this case, your throat will fall asleep over here and you will never get well okay woken up because this is a once in a lifetime event. If you miss it. Nobody wakes you up anymore. So one question will be, is this a deadlock. 80 00:10:47.700 --> 00:10:54.960 William Cheng: Okay so deadlock, meaning that you want that will never make any progress anymore. So typically involve more than one thread. So in this case, this is not a deadlock. 81 00:10:55.620 --> 00:11:05.910 William Cheng: Okay, so, so this is actually no, it's a race condition. So you have two threads that are racing where I want to, I tried to signal the condition and the other one is trying to unlock the mutation go away for the event. 82 00:11:06.390 --> 00:11:14.580 William Cheng: Okay, if it turns out that the, you know, sort of, if you're lucky, the timing just worked out everything will work. But if the bad timing happened, then it's not going to work. 83 00:11:15.030 --> 00:11:28.380 William Cheng: Okay, so that will be called a race condition the race condition is that it's a bad timing dependent behavior. So to threats are racing if once I get there first and the other third get a second, everything will be fine. But if it's the other order then bad things going to happen. 84 00:11:29.580 --> 00:11:36.240 William Cheng: OK. So again, this is not a deadlock, but instead it's a race condition. Okay, so you should never write code that has a race condition in them. 85 00:11:36.750 --> 00:11:39.870 William Cheng: Okay, or if you use function, you know, don't use a function that you can 86 00:11:40.260 --> 00:11:49.560 William Cheng: Use the functions incorrectly or something like that, you're going to end up with a race condition, we have a race condition, then sometimes your program works, and then your program. What will solve your problem. When I work 87 00:11:50.400 --> 00:11:53.340 William Cheng: Okay, so you should always make sure you don't write code with the race condition. 88 00:11:54.150 --> 00:12:01.290 William Cheng: Okay, sometimes race condition, a little tricky to DDT to to detect because only we are very unlucky, then the race condition will happen. Yeah. 89 00:12:02.220 --> 00:12:15.270 William Cheng: That that that particular analogy is kind of similar to do to della when you're unlucky you tell us what happened but della has those four things that the polls requirement over here, this case over here. It's a race condition. So it doesn't really satisfy those four conditions. Okay. 90 00:12:17.520 --> 00:12:20.130 William Cheng: All right, so sort of going to just summarize 91 00:12:20.490 --> 00:12:27.660 William Cheng: You know the synchronization requirement, right. So I'm going to sort of go through some of the rules as you know sometimes people forget that doesn't include all the rules. 92 00:12:27.870 --> 00:12:31.980 William Cheng: Just some of the important ones. Okay, so again you need to follow all the rules. If you want to make sure that 93 00:12:32.490 --> 00:12:37.740 William Cheng: You know that the thread work well together. Right. You know, so if you think about your warm up to your warm up to you're going to create for threats. 94 00:12:38.010 --> 00:12:43.650 William Cheng: All these forthright are working together to achieve the goal of simulating the token bucket filter. 95 00:12:44.070 --> 00:12:53.730 William Cheng: Okay, so they're helping each other. So they all know what each other is doing. OK. So again, the way they should do this is that you need to make peace with a function calls you need to use share variable for them to communicate with each other. 96 00:12:54.270 --> 00:13:05.130 William Cheng: Okay, so remember the threat you know that the theater experience that one CPU. So if you have one threat I ceilings that one CPU another activity and the other CPU. Well, there's no way for you to find out what the other side is doing. 97 00:13:06.450 --> 00:13:12.720 William Cheng: Okay. The other side is actually another piece of hardware. If you're running one piece of hardware. There's no way for you to find out what the other piece of horror is doing. 98 00:13:13.710 --> 00:13:20.370 William Cheng: Okay, so therefore, again, we need to write our co very, very carefully to make sure that no matter what happens, the co always work. Yeah. 99 00:13:20.940 --> 00:13:27.120 William Cheng: Alright, so again, here's a quick summary. Again, the picture that you should think about is this right there is a new tax right every new tech industrialization box. 100 00:13:27.330 --> 00:13:30.780 William Cheng: Instead of civilization box you put all the variables in there that will affect the guard. 101 00:13:30.990 --> 00:13:40.440 William Cheng: Or you put a bunch of critical section code in there, the critical section co will evaluate the guard would change the value of ABC. They will also call function to to use a condition variable. 102 00:13:41.040 --> 00:13:44.130 William Cheng: Okay, so that's, you know, that's what the critical section code is doing now. 103 00:13:44.670 --> 00:13:54.570 William Cheng: Alright. So, with respect to a new tax a threat can be. Well, actually, if you think about, you know, if you think about the meat is you gotta, you gotta remember their civilization box and condition variable Q, all this kind of stuff. 104 00:13:54.960 --> 00:14:08.550 William Cheng: So we're kind of threat be there. So a third can be executed inside of civilization box right that's one place for threat to be okay. A third can also be sleeping in the new tech school over here. Okay, or I can be CP instead of condition variable cute. 105 00:14:09.690 --> 00:14:19.050 William Cheng: Okay, where I was going to therapy, whether that can be outside. Right. The second be also outside over here. Okay, so there are only four places the threat can be okay. It's either here or here or here is either here or here or here. 106 00:14:19.230 --> 00:14:24.960 William Cheng: When it's outside. Well, maybe it's involving with with another new tax, you know, dealing with another condition variable. 107 00:14:25.410 --> 00:14:33.300 William Cheng: Okay, so in this case when it's outside. That means that it's outside of this civilization box. Okay. And it's outside this mutates outside this condition condition variable cute. 108 00:14:34.170 --> 00:14:39.060 William Cheng: Okay, when it's outside, it might be interacting with some other threads and using another matrix. 109 00:14:39.720 --> 00:14:52.800 William Cheng: Yeah, well. So again, this is very, very important though to understand there's no way for threat to be simultaneously in the new tax queue and inside the civilization box you can only be in one place at a time. Okay. All right. 110 00:14:53.850 --> 00:15:01.590 William Cheng: With respect to a new tax ABC right i mean i put them in a box over here instead of civilization box right they are variable that can affect the value of the guard. 111 00:15:01.950 --> 00:15:16.650 William Cheng: Okay, you can. So when you draw a picture like this you are making a note to yourself to say that you can only access, which means read and write or read or write. You can only read. All right, ABC Eve. The threat is inserted summarization box if the if the threat has the new timeslot 112 00:15:17.760 --> 00:15:28.110 William Cheng: Okay, so how do you know the SME tax law right if you return for P threat. I mean, tax law. What that means that you have to be a Glock. If you return from piece or a condition. Wait, you also have the new tax law. 113 00:15:29.820 --> 00:15:39.030 William Cheng: Okay, so there's two way for you to have the text right you return from piece of music, or if you return my piece of the nation way. So, in both cases, you see, you have to be tax law. Okay. 114 00:15:41.040 --> 00:15:48.300 William Cheng: All right. When you signal a condition variable right once we're in the condition variable. Q. Get moving to the matrix you and that's all that happened. 115 00:15:48.570 --> 00:15:52.680 William Cheng: Nothing else has happened right so it's my fault to listen link my photo is a pen. 116 00:15:52.890 --> 00:16:00.240 William Cheng: That's all that happens. Okay, when you broadcast the condition variable. The only thing that happened is that all the threatening condition variable. Q Can move into the meat ax, you 117 00:16:00.570 --> 00:16:04.290 William Cheng: Right on link all and then a Panda, Panda pan right that's all that happens. Yeah. 118 00:16:04.920 --> 00:16:10.140 William Cheng: You can only get added to the condition variable. Q. If you have the new tax law right so the thread itself. 119 00:16:10.560 --> 00:16:16.890 William Cheng: Needs to execute inside this realization box over here. When the 30 South copies or condition way it gets added to the condition variable cute. 120 00:16:17.400 --> 00:16:25.530 William Cheng: Okay. And that will be the only way I can be added into the condition variable. Q Okay. If you right we're code that I made that happen. And make sure you don't do that. Yeah. 121 00:16:26.250 --> 00:16:35.700 William Cheng: You can only modify the variable inside the guard. If you have to meet x LA. You can only read the variable in the in the guard. If you have new tax law. You can only execute critical section code. If you have new tax law. 122 00:16:36.840 --> 00:16:44.400 William Cheng: Okay. So, okay, here are a few rules and then you have to remember all the rules that I will talked about if you want your code to work all the time. And this is the rule, you have to follow that. 123 00:16:46.860 --> 00:16:50.100 William Cheng: Alright, so let's take a look at how to implement a 124 00:16:50.940 --> 00:16:59.850 William Cheng: General garden come at okay general go to combat. Right. The syntax is when guard and they have a you know a command sequence over here. So I'm going to sort of claim that the way that you implement it, you know. 125 00:17:00.300 --> 00:17:10.410 William Cheng: Implement implement the garden command is the following. Okay. On the right hand side over here. It's just going to be DB Pisa function call, but this time we're going to use the new tax and condition variables. 126 00:17:11.190 --> 00:17:17.580 William Cheng: Okay, so what we're going to do is add the first thing that we need to do is that while we need to evaluate the guard, you know, for us to evaluate the guard. We have to have the new tax law. 127 00:17:17.820 --> 00:17:23.130 William Cheng: Okay, so therefore we have to call piece there any tax law first and then we try to evaluate the guard. 128 00:17:23.910 --> 00:17:32.820 William Cheng: Okay, if the god is true. What should we do, why is the guy who is true. We need to execute a command sequence, right. So we're here. There's an infinite loops. So while Nagar piece recognition way. 129 00:17:33.900 --> 00:17:34.200 William Cheng: Okay. 130 00:17:35.430 --> 00:17:45.990 William Cheng: Actually, I'm going to ask you to memorize this. Okay. The way you implement a garden command. The first three things you have to do is piece where new tax law, while not guard Peter condition. Wait. 131 00:17:47.190 --> 00:17:52.770 William Cheng: Okay, so just memorize that. Right. You know, and anytime you want to Punta Gorda command piece every tax law one RP forgiveness. You wait 132 00:17:52.950 --> 00:18:02.940 William Cheng: Okay, so what does well. Now go right so guard over here is a Boolean expression. So all you have to do is to put parentheses around the, the other the Boolean expression put an exclamation mark in front of it and then you're done. 133 00:18:04.290 --> 00:18:10.320 William Cheng: Okay, so you can easily convert a garden command in the pseudo code and convert it into sequel by, you know, 134 00:18:10.560 --> 00:18:19.440 William Cheng: By by putting the guard around parenthesis at an exclamation mark in front of it and put the keyword wall in front of it. And inside this while loop. There's only one thing that you do is that you copied or condition way. 135 00:18:20.880 --> 00:18:23.910 William Cheng: OK. So again, Peter. Peter o'clock. What are these are going away. 136 00:18:24.630 --> 00:18:35.340 William Cheng: Okay, so in. So in the case when the guard is false, what's going to happen, right. If the guard is false. Well, then we'll Naga will be true. So in that case, what you will do is that he will give us the music by calling Peter condition. Wait. 137 00:18:35.700 --> 00:18:43.020 William Cheng: There. So in this is, again, this is an atomic operation you unlock the new Tesla, you go to sleep in the condition variable. Q You want atomic operation. 138 00:18:44.190 --> 00:18:51.930 William Cheng: Okay, so later on when somebody else wake you up what's gonna happen. You're going to return from Peter condition where it over here when you return from Peter condition where we are, what do you do 139 00:18:52.920 --> 00:19:01.020 William Cheng: Okay, so since this call has no curly bracket over here. So when you come out with a piece of garbage away. The first thing you do is that you reevaluate the car right away. 140 00:19:02.250 --> 00:19:07.020 William Cheng: Okay, every time when you when you return from piece of condition way. The first thing you should do is to reevaluate the guard. 141 00:19:07.290 --> 00:19:18.510 William Cheng: Okay, if the guard is false. What do you do with regard is false. Again you copies of condition way. And next time, will you wake up you evaluate the guard. Again, if every time they evaluate the guard guard is false, you're going to keep say stay in this infinite loop forever. 142 00:19:19.620 --> 00:19:26.400 William Cheng: Okay. Is that okay, well, yeah, that's perfectly okay right so all we're going to hope is that eventually, you know, the car is going to become true 143 00:19:26.610 --> 00:19:33.150 William Cheng: Okay, if the guard never become true well then you're gonna you're gonna sleep in the condition verbal cue over here forever right temporarily you going to jump out of it. 144 00:19:33.540 --> 00:19:35.850 William Cheng: Okay, but then the guard is false. You're going to go back to sleep again. 145 00:19:36.600 --> 00:19:45.000 William Cheng: Now, what if the guard is true, right, the guard is true, then you jump out of the infinite loop when you come out of the infinite loop over here you can execute the current sequence that we hear from statement one to the end. 146 00:19:45.930 --> 00:19:49.650 William Cheng: Okay. And then what you're dying copy. So, Mr log and then you're done with the garden command. 147 00:19:51.300 --> 00:19:58.290 William Cheng: Okay, so therefore we should ask the question over here. The this does the sequel on the right hand side implement the garden command on the left hand side. 148 00:19:59.430 --> 00:20:11.310 William Cheng: Okay, so of course my claim is that the answer is yes. Right. The semantics of the left hand side says that when the god is true, you execute the commanded sequence in one atomic operation. Does that happen in this code. 149 00:20:12.630 --> 00:20:23.880 William Cheng: OK. So again, you call Peter I mean tax lock and now you're in the critical section. If the guard is true what happened right the guard is true. You skip the while loop over here, you actually recommend sequence all in the same 150 00:20:25.320 --> 00:20:33.420 William Cheng: You know, all in the same critical section code, right. So, therefore, seeing the guard to be true and excellent commands sequel is done in one atomic operation. 151 00:20:35.430 --> 00:20:40.290 William Cheng: Okay, so therefore the code over here, implement exactly the semantics and also if the guard is false. 152 00:20:40.530 --> 00:20:48.060 William Cheng: Nothing happens. Right. The Goddess was over here you go to sleep in a condition barbecue, and you wake up in the garden. God is false. Again, you go to sleep again and keep doing this. 153 00:20:48.540 --> 00:21:02.280 William Cheng: So therefore, nothing happens. Okay. You just, you just go to sleep. When you go to sleep over here inside P3, a condition where there's an implied P through me to unlock right because the unlocking the tax and then you sleep in the independent variable key one topic operation. 154 00:21:04.170 --> 00:21:09.420 William Cheng: Okay, so therefore the code on the right over here is an exact implementation of the pseudo code on the left. 155 00:21:10.740 --> 00:21:14.400 William Cheng: Okay, so. So on the right hand side over here. I mean, how many critical section code. Do you have 156 00:21:15.780 --> 00:21:19.920 William Cheng: I mean, it's a little tricky over here, right. You can actually see if you count very, very carefully. 157 00:21:20.190 --> 00:21:27.900 William Cheng: critical section coast any code between piece. Every time slot and piece of meat, an unlock and now we have an employee piece me to unlock and piece of music log 158 00:21:28.110 --> 00:21:37.200 William Cheng: Insight piece or condition way. Right. So at the beginning appeaser condition way. There's a lot. There's unlock. All right. Okay. And then the end the piece of condition where there's an employee lock right here. 159 00:21:38.250 --> 00:21:43.470 William Cheng: Okay, so therefore, in this case there is actually going to be for critical section code inside the code on the right over here, right. 160 00:21:43.650 --> 00:21:53.520 William Cheng: One critical section is that you locked in new tasks guard is false. You call piece of condition way but at the beginning of peace or confusion way. There's a piece of me to unlock. So, therefore, that will be one critical section right here. 161 00:21:54.510 --> 00:22:01.290 William Cheng: The second critical section is that you locked immune tabs over here. And it turns out the guard is true. So you actually have a command sequence overhearing automate tasks. 162 00:22:01.410 --> 00:22:06.060 William Cheng: So seeing the guard to be true and excellent command sequence that will be inside another critical section. 163 00:22:06.960 --> 00:22:15.150 William Cheng: Okay, where's the other one. Okay, when you return from piece or a condition where there's an implied piece for me tax law and then you see the guard to be false. 164 00:22:15.540 --> 00:22:22.950 William Cheng: Get there you go back to sleep again what, in that case, again you copies every town law. So there's another critical section right here where you see the guard to be false. 165 00:22:23.190 --> 00:22:28.890 William Cheng: And the last one over here is that will you return from Peter condition way you're gonna call piece. I mean, tax law or be equivalent to calling Peter 166 00:22:29.340 --> 00:22:36.420 William Cheng: New tax law and then you see the guard to be true and excellent command sequence and you all have to be attacks and that will be the fourth critical section. 167 00:22:37.500 --> 00:22:45.180 William Cheng: Okay, so again this code is very, very tricky, you know, even though it's only a few lines of code over here, you actually contain for, you know, for different critical sections. 168 00:22:46.230 --> 00:22:55.560 William Cheng: Alright, so again you know you need to understand exactly what this code is doing and convince yourself that the good, the sequel. On the right is an exact implementation of the garden command on the left there. 169 00:22:56.130 --> 00:23:07.980 William Cheng: All right. What if you just want to modify the guard. Right. So in this case, you don't have you don't have a guard right we saw that before you know in the V operation, you don't have a guard so therefore it is possible, you know, all you want to do is that you want to 170 00:23:08.310 --> 00:23:15.120 William Cheng: You know, you, you, you want to modify the god. OK. So again, how do you modify the car right you modify the variable ABC there. 171 00:23:15.510 --> 00:23:21.660 William Cheng: Okay, so in order for you to modify them the rule does that you have to, you have to lock the new tax and then you make the changes and the 172 00:23:21.990 --> 00:23:28.830 William Cheng: Matrix. Okay, so therefore the implementation is gonna be on the right over here. So the first thing that you do is that you unlock the you like to eat eggs. 173 00:23:29.310 --> 00:23:35.520 William Cheng: Okay, you need to wait for the meantime, any way to be to you for you to own the new tax because only after your enemy attacks, then you can change a, b, and c 174 00:23:36.000 --> 00:23:44.700 William Cheng: There. So what you will do is that you lock the new tax and then when this function returns you know that you only meet x. And now you can modify the guard you can modify a, b, and c 175 00:23:45.180 --> 00:23:58.680 William Cheng: OK. And then when you're done over here right before we unlock the meat as well maybe if there's a thread weightings or condition variable. Q. That's where it needs to reevaluate the guard. Okay. Why does that need to be need to really evaluate guard because you have changed. A, B, or C. 176 00:23:59.760 --> 00:24:08.700 William Cheng: Okay, so you are the thread that comes over here. You're going to change. A, B, or C, right. Why did you change a B or C, you need to wake up you know the other thread that might be a sleeping in the condition verbal cue. 177 00:24:09.480 --> 00:24:14.640 William Cheng: Okay, so therefore you have a choice. Now you can either signal the condition or broadcast the condition, which one should you use 178 00:24:15.240 --> 00:24:19.740 William Cheng: OK. So again, this depends on your program. Okay. You need to understand what your program logic. 179 00:24:19.920 --> 00:24:29.520 William Cheng: And you will decide that. Should I wake up, only one thread or should I wake up all the threats. Right. They don't going to see an example, under what condition you wake up one day and what condition you wake up all the threats there. 180 00:24:29.730 --> 00:24:41.730 William Cheng: And then we are done over here. So again, you can only signal Brock has a condition where you have to monetize law. So after you do that you can unlock the matrix that if you don't follow all these rules over here, you're going to end up the race condition. 181 00:24:42.810 --> 00:24:50.820 William Cheng: So again, if you write your code in the pseudo code garden command right when you have a garden command, you have to employ you just directly translate into sequel, just like this. Following the recipe. 182 00:24:51.030 --> 00:24:54.810 William Cheng: And if you want to modify the variable A, B and C. Again, you follow the recipe over here. 183 00:24:55.680 --> 00:24:59.520 William Cheng: Okay, so the only thing that you have to do. Now you have to sort of figure out whether you should signal Brock has condition. 184 00:25:00.300 --> 00:25:03.360 William Cheng: Okay. Otherwise, you know, you just follow the recipe, then 185 00:25:04.140 --> 00:25:11.880 William Cheng: Alright, so let's take a look at example as, by the way, again, do not believe that piece recognition signal broxtowe can be called without locking the meat ax. 186 00:25:12.240 --> 00:25:23.700 William Cheng: Okay, you're going to search the internet when you search the Internet, you're gonna find out some people is going to argue that you can call piece where condition signal a piece of condition broadcast without the mutates being locked, should you believe them. 187 00:25:25.530 --> 00:25:31.680 William Cheng: Okay, I'm the end. I mean, I'm going to tell you they don't believe them. Okay. You always write your code so that you call Peter condition signal Peter condition will 188 00:25:32.010 --> 00:25:36.000 William Cheng: Broadcast with the new tax law, then your co will work no matter what. 189 00:25:36.840 --> 00:25:42.870 William Cheng: Okay, so why would people actually like that. No lie, lie, lie, lie like that on the internet to say that. Yeah. You don't have to call them. Okay. 190 00:25:43.170 --> 00:25:47.970 William Cheng: The reason for that is that maybe they are talking about a specific implementation of the P3 library. 191 00:25:48.360 --> 00:25:57.030 William Cheng: Okay, maybe there are 200 about is this piece or a library on Ubuntu Linux to point you know a six or something like that in that particular implementation, you can do that. 192 00:25:57.660 --> 00:26:00.600 William Cheng: Okay, but what later on. But to actually change your implementation. 193 00:26:00.870 --> 00:26:11.700 William Cheng: Well, then there's no guarantee. Right. So if you want to be perfectly safe you always, you know, copy me tax signal broadcast. Well, you have to meet at law that it doesn't really matter who implement that that will always work. 194 00:26:13.320 --> 00:26:22.500 William Cheng: All right. Otherwise, you need to find out exactly what people are talking about maybe they're telling the truth on a particular version of, you know, maybe it's window maybes Mac OS. So whatever that is. 195 00:26:22.770 --> 00:26:29.040 William Cheng: They need to be very specific to say that you know under what operating system implementation or what piece of every implementation, they're guaranteed to work. 196 00:26:29.520 --> 00:26:38.310 William Cheng: OK, so maybe if it when certain piece or library or implemented certain organism, they guarantee the work. But in general, there's no such guarantee. Okay, so you should always do the right thing. Okay. 197 00:26:40.050 --> 00:26:49.050 William Cheng: All right, so the condition variable. You can also initialize it right you can Angela initialize aesthetically, if you have a local variable. If you have a global global you'll do it this way. 198 00:26:49.290 --> 00:26:57.360 William Cheng: If you're going to call Malik and freeway in that case. Why is it called Malik. They are initialized. So you have two copies of condition in there. And then we are done with a you couldn't copy the condition 199 00:26:57.630 --> 00:27:08.820 William Cheng: Destroy and then you can free up the data structure. Okay, for the purpose of warm up to you only need one new tax you only need one condition variable. So, therefore, don't call Malik on the consumer, but because there's only one 200 00:27:09.570 --> 00:27:22.170 William Cheng: Okay, so you can. What you should do is that you create a global variable. One of them is a new tax and the other ones condition variable and you initialize it using static initialization. Okay, so this way you know you don't really have to worry about, you know, doing something else. Right. 201 00:27:27.330 --> 00:27:34.110 William Cheng: Alright, so now we're ready to implement the readers and writers problem. So what is the reader and writer problem. So imagine that you have a database system over here. 202 00:27:34.590 --> 00:27:36.870 William Cheng: I'm going to say that this system is huge is slow. 203 00:27:37.080 --> 00:27:43.260 William Cheng: And we're gonna have a lot of threads over here, try to read the database, right. It's just like a web application over here. You know when you come to your web server. 204 00:27:43.440 --> 00:27:50.970 William Cheng: You're going to talk to the backend database to say that I want to read some data that. So in this case, you're gonna end up with many, many threads. I want to read simultaneously. 205 00:27:51.510 --> 00:27:57.990 William Cheng: OK. So the rule is over here is that, you know, sometimes you have something I want to write to the database and sometimes you have you have says I want to read the database. 206 00:27:58.350 --> 00:28:05.010 William Cheng: Okay, so one thing that you can do is that you can actually lock up the entire database using a new tax and only allow one thread to come to the database at a time. 207 00:28:05.400 --> 00:28:15.720 William Cheng: Okay, so that will work. Okay. But again, that's not very, very efficient. Okay. You want maximal parallelism. Right. Okay. So, so let's see that. I don't have any writer threats that are out there that wants to write at the same 208 00:28:16.440 --> 00:28:25.740 William Cheng: At the current time. Okay. All I want to do is I want to refund the database, right, there's 100 readers search over here. They all want to read the they'll want to read the database. Are they allowed to read in parallel. 209 00:28:26.730 --> 00:28:33.840 William Cheng: Or the answer, of course, is yes, you know, because you know they're reading in parallel. So therefore, they will not be able to step on each other's toes. 210 00:28:34.620 --> 00:28:42.090 William Cheng: Yeah, so therefore, in order for me to have maximum parallelism. I'm gonna allow one all these readers search over here to read some autonomously inside the database. 211 00:28:43.200 --> 00:28:50.520 William Cheng: Okay, but what about if there's one writer threat right if there's one writer side over here I have 100 readers read over here, then I have a decision to make. Do I want the right 212 00:28:50.970 --> 00:28:55.290 William Cheng: What's up when the writer is writing data. Okay. Should the reader be reading data at the same time. 213 00:28:55.800 --> 00:29:06.270 William Cheng: Obviously not right because this thread when I tried to modify the database. It's going to make multiple queries going to change the database left and right. So if you have a reader. So I tried to read the data at the same time, while the reader is going to read inconsistent data. 214 00:29:06.990 --> 00:29:11.640 William Cheng: Okay, it's almost like you're manipulating the link lives and somebody else tried to read the linguists, we're going to end up the segmentation fault. 215 00:29:12.060 --> 00:29:19.290 William Cheng: Okay, so again this is a database example over here. So, so when a threat wants to write the data. What, then, none of the reader through actually be reading the data. 216 00:29:20.580 --> 00:29:31.650 William Cheng: Okay, so therefore, when you have one writer and 100 readers are they all want to access the database at the same time. Well, then you have a choice. Do you want to let the writer so right to the data or you want to have 103 I try to read the data simultaneously. 217 00:29:33.030 --> 00:29:42.960 William Cheng: Okay, you may have more writer so I want to write. So again, when the writer third right the data over here, the only ones that can be writing at the same time. But when you want to have a bunch of tried to read the data they can all read in parallel. 218 00:29:43.560 --> 00:29:48.930 William Cheng: Okay, so this way you have maximum Pell is m and also you have mutual exclusion, where you are required to have them. 219 00:29:49.500 --> 00:29:58.980 William Cheng: Okay, so when you're writing only one writer threat can be right to the data time and will you read, you should have maximum palace. Right. And when you're reading the data, none of the writers should have access to the data. 220 00:30:00.270 --> 00:30:03.090 William Cheng: Okay, so the question is, how do you implement this right 221 00:30:04.590 --> 00:30:09.030 William Cheng: All right, this solution over here is super simple. If we use Carter command. 222 00:30:09.630 --> 00:30:15.330 William Cheng: Okay, so this is the function call reader any reader thread wants to refund the database, they will call it will call this function. 223 00:30:15.720 --> 00:30:21.750 William Cheng: Okay. Here's a function called writers any writer threats. They want to write to the database. They will also called they will also call this function. 224 00:30:22.560 --> 00:30:31.830 William Cheng: Alright. Alright, so, so, so the my example here, I have 100 reader threads over here, try to read up on a database. I'm going to end up with like three threads over here. 225 00:30:32.280 --> 00:30:38.820 William Cheng: They tried to write to the database, I guess, in this case what will happen over your eyes. Okay, you look at the code over here for the readers, so 226 00:30:39.570 --> 00:30:43.560 William Cheng: Let's forget about writer for now let's just only look at the readers, as a matter easier. 227 00:30:44.100 --> 00:30:52.470 William Cheng: The reader over here says, You know, I'm going to use some variable in the guard over here. One is called a number of writers in the number of writers equal to zero, that means that nobody's writing to the database. 228 00:30:53.040 --> 00:30:56.400 William Cheng: Okay, so if you if you pick a, you know, take a peek on the code on the right over here. 229 00:30:56.580 --> 00:31:04.470 William Cheng: When the writer wants to write to the database. So I'm going to increment the number of writers. So the writers over here can be equal to zero or one, right, because I can only allow one writer to be writing at the same 230 00:31:04.890 --> 00:31:12.150 William Cheng: Time. So therefore, the writers variable be here. It's kind of like a semi for but it's not a semi for. Why is it not a semi for 231 00:31:13.230 --> 00:31:19.590 William Cheng: That because if it's a semel for then you have to talk about the P and V operation. Right. So in this case, you look at the code over here. There's no PM be 232 00:31:20.520 --> 00:31:28.230 William Cheng: Okay, so therefore we're not using Salesforce. OK, so the writers over here at behave like a binary said before, because it go from zero to 1100110 233 00:31:28.770 --> 00:31:38.130 William Cheng: Okay, so, so, so it's kind of like a binary symbol, but it's not a binary sevenfold yeah alright so when the number of writers over here you go to zero. Well, then, in this case, the writer and the reader can come in and read 234 00:31:38.520 --> 00:31:48.450 William Cheng: Guys, so therefore we're going to do is that, you know, when the writers equal to zero. I'm going to increment a number of readers over here. And then we finished the command sequence. So therefore, we're going to come inside over here and read 235 00:31:49.410 --> 00:31:57.660 William Cheng: Okay, so if there are no writers over here, all these 100 either thread, they all, you know, very soon they all going to come in the middle of here and they all going to read in parallel. 236 00:31:58.920 --> 00:32:09.000 William Cheng: Okay, because all of them over here. We'll see. Ready, go to zero. And then they will increment the reader. So in the end, we're going to have, you know, the readers all running parallel over here and the number of readers over here is going to be 100 237 00:32:09.780 --> 00:32:14.250 William Cheng: Okay, so by reading the number of readers over here you will see how many readers are reading over here in parallel. 238 00:32:14.730 --> 00:32:26.100 William Cheng: Okay. And then, you know, they all read a different speed, right, someone who's gonna go first. I'm gonna later. So when they go out, they would recommend a number of readers over here so you can always keep track of the number of readers that are actively doing the reading. 239 00:32:27.390 --> 00:32:33.210 William Cheng: Okay, so eventually when all of them leave over here that's going to go to zero. And in the meantime, when they're leaving more readers can come in. 240 00:32:33.270 --> 00:32:41.280 William Cheng: Right. One more reader can come in again number right is equal to zero. So, they will they will again there will be added that so as long as you have readers. Keep coming in and they will continue to read in parallel. 241 00:32:41.820 --> 00:32:44.220 William Cheng: Okay, so that's the maximum amount of parallels. If I can have 242 00:32:45.120 --> 00:32:53.910 William Cheng: Yeah. Alright, so let's take a look at the writer codes over here. So let's say that we have no writers at this time. There's a bunch of readers over here that are reading. So let's get a number of readers over here equals two, two. 243 00:32:55.230 --> 00:33:03.900 William Cheng: Okay, so now the writers coming over here. And then what it will do is it will execute discard command when the number of writers equal to zero and the one and number really equal to zero and then 244 00:33:04.260 --> 00:33:07.260 William Cheng: The card is true. And then the writers over here. We're going to increment it from zero to one. 245 00:33:08.310 --> 00:33:14.820 William Cheng: Okay, so at this point that number readers will be equal to two. So therefore, the writer is being blocked right here. Right. So this word over here is going to get blocked or 246 00:33:15.060 --> 00:33:22.890 William Cheng: Guarded command and then eventually what the story is going to leave one at a time. Eventually a number reader over here. He's going to get to zero when they go to zero, then the guard is true. 247 00:33:23.220 --> 00:33:31.740 William Cheng: Right. So therefore, the number of writers over here you go to zero, then I will read or equal to zero over here. So then we're going to execute atomic operation over here to incremental or number of writers to one. 248 00:33:32.820 --> 00:33:39.270 William Cheng: Okay, so now the writer will be executing code over here, right, because you're done with the garden command and now you're right. So it's going to be actually go over here. 249 00:33:39.690 --> 00:33:46.950 William Cheng: What if a reader that comes in at the time. At this time, when I read this, I come in at this time over here, the reader, so it will get blocked because the number of writers over here you go to zero. 250 00:33:47.400 --> 00:33:55.920 William Cheng: Okay, another really common. Another beautiful setup, they all get blocked over here because the number of writers over here you go to one. So they all got a blog at the beginning of the quarter command over here. 251 00:33:56.670 --> 00:34:05.100 William Cheng: Okay what if more writers threat comes in. Right. So another writers that comes in. Over here you will see that the number of writers equal to one. So, therefore, other writers threads over here, they all get blocked. 252 00:34:05.880 --> 00:34:12.480 William Cheng: Okay, so your throat over here it's going to end up, you know, writing to the database is going to take its time and do all this kind of stuff. 253 00:34:12.720 --> 00:34:21.330 William Cheng: Eventually when this thread is done what he would do is that I will execute the bottom garden command by detriment. The number of writers over here will go to zero as soon as it goes to zero happen. 254 00:34:22.440 --> 00:34:30.450 William Cheng: Okay, so we have a bunch of reader thread, they're all trying to get in and we also have a bunch of right writers to over here to try to get in. One of them is going to be able to win. 255 00:34:31.020 --> 00:34:39.900 William Cheng: Okay, we don't know which one. Maybe the reader, maybe the writer so if if it's the reader is going to win. Once they read it comes in over here. We're going to increase the number of readers over here. Why, in that case. 256 00:34:40.140 --> 00:34:45.870 William Cheng: All the writers over here will get blocked. Okay, so if it's just so happened that the reader is coming first all the reader is going to come in. 257 00:34:46.590 --> 00:34:54.450 William Cheng: Okay, because the reader will read in parallel. Right. So as soon as the first reader comes in that all the reasonable coming to come inside in the middle. Over here you all read in parallel. 258 00:34:55.170 --> 00:34:59.460 William Cheng: Okay, if it turns out that is the other case right when you leave you know the code over here. 259 00:34:59.850 --> 00:35:08.970 William Cheng: The number of writers have equal to zero. If the writers come in first. Now that number right equal to one, that all the readers over here will continue to get blocked and also the rest of the writers over here. They will also give off. 260 00:35:11.250 --> 00:35:18.780 William Cheng: Alright, so, so when you read this code, this is what's going to happen. OK. So again, this code is a little tricky. Make sure you understand exactly what's going on. Yeah. 261 00:35:19.560 --> 00:35:21.930 William Cheng: Alright, so again this is a synchronization code, you have 262 00:35:22.290 --> 00:35:34.320 William Cheng: This. You're not the first procedure for the reader through the writer threat the reader and writer. Third, they're doing something else when they try to synchronize they call these functions over here to read and write to refund the database or write to the database, then 263 00:35:37.200 --> 00:35:41.850 William Cheng: Okay, this is in a textbook. I'm not gonna skip it over here. Okay. 264 00:35:42.420 --> 00:35:44.670 William Cheng: All right, so, so we're here. 265 00:35:45.060 --> 00:35:51.240 William Cheng: You know, so we saw the sequel before, right. So when you just change the variable in the garden. So where are the variable in the guard. 266 00:35:51.420 --> 00:35:58.500 William Cheng: Right. So in this example here we have two guards one guard is right here. Right. The other guard is right here. So, so we have two different guards over here. 267 00:35:59.190 --> 00:36:06.750 William Cheng: Okay. So inside the guard over here. There are different variables, get involved. There's a reader. And there's a writer. So in this case, there are multiple gods. So they'll get you need to be very, very careful. 268 00:36:07.080 --> 00:36:15.210 William Cheng: Yeah, alright. So, so when you detriment. A number of readers over here. Okay, so the question is that, are you supposed to signal or broadcast the condition 269 00:36:15.600 --> 00:36:22.230 William Cheng: What we saw before, right, for the garden, you know, for you know for for the one that has a guard. It's very easy to convert them into sequel. 270 00:36:22.860 --> 00:36:28.050 William Cheng: Okay. The only question is that for the one that doesn't have a guard, you know, are you supposed to signal Brock has a condition 271 00:36:28.980 --> 00:36:40.170 William Cheng: That. So let's take a look at examples over here. Let's see the number of readers will be equal to zero or equal 100 okay we have 100 sales over here running parallel guy. And now we're going to document them. One of them is done according to 99 272 00:36:41.520 --> 00:36:49.560 William Cheng: Okay, so then the reader over here appear inside. Discard any theories are, you know, if there are three sleeping over here. Do you need to wake it up. 273 00:36:51.690 --> 00:37:00.360 William Cheng: Okay, so, so we, here we have writers over here that are actually sleeping over here. So when the number of readers go for 102 99 if you wake up the writer through what's going to happen. 274 00:37:01.020 --> 00:37:10.230 William Cheng: While the writer is going to bed will happen is it's going to get woken up. And as soon as you wake up, you will see that the number of readers, you go to 99 okay so therefore you will go back to sleep. 275 00:37:11.490 --> 00:37:19.140 William Cheng: Okay, so therefore, even though. So, so again, what's important to understand that over here that you change the number of readers. Well, there's no reason to wake up any of the writer threat. 276 00:37:19.740 --> 00:37:21.930 William Cheng: Right, because if you wake them up, they're gonna go back to sleep right away. 277 00:37:22.410 --> 00:37:31.380 William Cheng: Okay, what did you go detriment from 90 1998 right. So again, there's no point for you to wake up in a reader through as I wake up any writers to add because as soon as you wake it up is then go back to sleep again. 278 00:37:31.650 --> 00:37:39.600 William Cheng: Okay, eventually, when you go from one to zero. We go from one to zero in this case. Well then, when you wake up a writer threat, then that will be worthwhile. Right. 279 00:37:39.750 --> 00:37:44.280 William Cheng: Because that's what i when he wakes up over here you will check the number of readers equal to zero. And now you can actually come in. 280 00:37:46.380 --> 00:37:51.900 William Cheng: Okay. So, therefore, you know, this is the CO. I mean, again, this code is inside of one process. Right. So, therefore, if you're the programmer. 281 00:37:52.050 --> 00:38:01.170 William Cheng: You're writing code for both the reader and writer. So, therefore, if you are documenting the number readers over here, you actually can see the critical section code, you can see the Garda command in another threat. 282 00:38:02.040 --> 00:38:12.630 William Cheng: Okay, so therefore you need to look at this Cotto said, oh, you know, if the number of readers over here doesn't get that committing to a zero, I should never wake up any threats, I should I should never wake up any writer threat. 283 00:38:13.500 --> 00:38:18.450 William Cheng: Okay, only when the number of readers, go to zero, then it will be worthwhile for me to wake up a writer sweat. 284 00:38:19.530 --> 00:38:22.890 William Cheng: Okay, so then this guy is again what I use signal over the conditional progress condition. 285 00:38:23.970 --> 00:38:32.580 William Cheng: I mean, you know, again, by looking at this code over here to number radical from zero to 101200 to one so never go beyond one. So, therefore, if I wake up, all three threads. 286 00:38:32.970 --> 00:38:40.470 William Cheng: Okay, there are three threads over here that are waiting. You know, they actually are sleeping. If I wake up three of them, only one of them will actually go over here and the other two will go back to sleep. 287 00:38:41.400 --> 00:38:49.260 William Cheng: Okay, so therefore, again, there's no point for me to do with Peter condition broadcasts awake them all up as you only copy. So condition signal to wake up one writer threat. 288 00:38:50.520 --> 00:39:00.150 William Cheng: OK, so again by understanding what your code is doing. You get to decide whether you want to do. Peter condition signal or you want to do. Peter condition broadcast and you get to decide when you want to do that. 289 00:39:00.870 --> 00:39:15.930 William Cheng: Okay, so every time when you change the guard. You don't have to wake up threats. OK. So again, to be more efficient. You should try to, you know, you can make this as efficient as possible so you don't wake up at 3am and that's a fairly sort of we'll go back to sleep right away that 290 00:39:17.250 --> 00:39:25.620 William Cheng: All right, what about this writers over here, right, this writer is over here is involved with two guards over here. Okay, so again this writer over here when I documented it go from one to zero. 291 00:39:26.520 --> 00:39:32.010 William Cheng: Okay, when it go from one to zero over here why in this case for the reader, they will see the car to be true. 292 00:39:32.730 --> 00:39:38.190 William Cheng: OK. So again, the idea here is that in the guard can go from false to true whether, in that case you need to wake up those threats. 293 00:39:38.430 --> 00:39:46.320 William Cheng: Okay, if the guard can go from two to four hours, why you don't really worry about that. But in this case, when it goes from one to zero this God can go from false true. What about this card over here. 294 00:39:47.130 --> 00:39:54.780 William Cheng: What if the number of readers over here just happened to be zero. Then in this case this car can also go from, false, true, true, right. So, therefore, in this case, you should wake up the readers, but 295 00:39:55.080 --> 00:39:57.990 William Cheng: Should you copy the condition signal you should copy definition broadcast 296 00:39:58.920 --> 00:40:07.410 William Cheng: But again, if there is a lot of readers says over here. They're all waiting to come in over here, you should wake them all up, so therefore you should call peaceful conditions broadcast to wake up all the readers were 297 00:40:07.740 --> 00:40:14.190 William Cheng: What about writers who has over here, right. So to get only one of them can come in, so therefore you will call peaceful conditions signal on the reader to read 298 00:40:15.390 --> 00:40:15.660 William Cheng: Them. 299 00:40:16.980 --> 00:40:28.530 William Cheng: All right, finally about these readers over here. Again, your increment the reader over here. When you increment a reader the garden go from, false, true, true, but you can never go on site, you can go from two to false, but can never go wrong with also to 300 00:40:28.740 --> 00:40:36.450 William Cheng: Increment a number of writers over here again the guard over here can only go from, you know, to, to false but not from false or true. So in this case, you don't have to wake up anybody 301 00:40:37.410 --> 00:40:42.090 William Cheng: Okay. So in this case, again, you only need to wake up somebody where you are executing the code at the bottom over here. 302 00:40:43.740 --> 00:40:45.330 William Cheng: Okay, finally. 303 00:40:47.520 --> 00:40:58.200 William Cheng: Okay, so if you guys see someone mentioned over here, the number of writers over here behave like a binary center for because it go from zero to 1100110. Okay, what about a number of readers over here. 304 00:40:58.470 --> 00:41:05.640 William Cheng: It behave just like accounting staff before. Right. They go from 0123 to a high number. And then it comes back down. It can never go to zero. 305 00:41:05.850 --> 00:41:15.990 William Cheng: It behaves like a county some of what, but is it a county semi for the answer is no, because if it's a county center for then the only allowed operation on it is going to be p operation and the operation. 306 00:41:16.890 --> 00:41:23.970 William Cheng: Okay, and there's no POV over here where we implemented a generalized Carter command over here, so therefore they are not semi force. Yeah. 307 00:41:24.870 --> 00:41:29.790 William Cheng: Right. Finally, how do you actually wake up all the readers threat and don't catch the writer threat. 308 00:41:30.570 --> 00:41:39.810 William Cheng: Okay, if they all sleep inside the same condition variable Q, whether you can only you can only wake up one of them and you don't really have any control, who do you wake up right we go Peter condition signal. 309 00:41:40.080 --> 00:41:46.590 William Cheng: On a tool that has reader thread and writer. So now you got to pick up a random swear, and you wake it up so that one could be a reader. So it could be a writer for that. 310 00:41:47.220 --> 00:41:52.350 William Cheng: Okay. And again, if you want a cop condition broadcast. You're going to wake up all the regions where you wake up all the writers were 311 00:41:52.920 --> 00:41:59.610 William Cheng: Okay, so one thing that you can do. So you can actually keep the reader threat in one condition variable queue and keep the writer threat in a different condition variable cute. 312 00:41:59.940 --> 00:42:07.530 William Cheng: Okay, so the pages are look like this. Now, right, you have a mutation virtualization box and now you have to condition variable cue to condition variables over here. 313 00:42:07.830 --> 00:42:11.610 William Cheng: One of them for the reader threat to sleep on and the other one is for the writer threat asleep. 314 00:42:11.940 --> 00:42:15.930 William Cheng: Okay, so this one I'm going to call this committee verbal cue the readers queue. 315 00:42:16.230 --> 00:42:25.620 William Cheng: Right. So this way when the reader wants to fall asleep. They all sleep inside this cute right. The second key of yours know as the writers. Q When the writer threads over here want to fall asleep, they will go into the skew. 316 00:42:26.730 --> 00:42:33.570 William Cheng: OK, so again. Well, you need to write your code need to write your code, very, very carefully. So, so the reader. So, and the writer, they will they will fall asleep inside, right, is that correct 317 00:42:33.870 --> 00:42:45.900 William Cheng: Right. So this way when you are executing the coding side over here if you change the value of the guard you can call piece or a condition broadcast to wake up all the readers, where you can copy. So condition signal to wake up, only one of the writer threat. 318 00:42:46.680 --> 00:42:54.000 William Cheng: OK. So again, this one, this will give you fine grained control of your parallelism. Okay, and find one control is what we're looking for that. 319 00:42:56.340 --> 00:42:56.700 William Cheng: Alright. 320 00:42:57.060 --> 00:43:06.120 William Cheng: So a reminder. Again, if you see a garden command over here on the right hand side over here is our implementation, right, if you see a garden command without the guard one in that case on the right hand side of your implementation. Okay. 321 00:43:06.270 --> 00:43:11.130 William Cheng: So what we're going to do that, we're going to take this call right here and we're going to directly converted into sequel. 322 00:43:11.790 --> 00:43:21.870 William Cheng: OK. So again, they are for God commands over here, right, two of them has guard there on the top over here. And then we have to read and the right over here. And then at the bottom over here. We're going to have to blog article mount without the guard. 323 00:43:23.070 --> 00:43:31.320 William Cheng: Guys, by the way, it's very important to understand over here is that the read and the right over here. The way that we implement our code over here when you perform the read over here. Do you have the meatheads lot 324 00:43:32.970 --> 00:43:39.450 William Cheng: Okay. So in this case, all these stories are working together. So there's going to be only one new tax right here's one new tax. There's a reader skew. There's a vital role, what rescue 325 00:43:39.690 --> 00:43:47.400 William Cheng: Lots of critical sections over here, right in the guard over here, the variable instead of guards to read the number of readers and a number of writers over here. 326 00:43:47.790 --> 00:44:00.120 William Cheng: Okay, so when you perform the read and write operation when you're reading over here you have the mutants unlocked right what how do you know that, right, because if you don't have the meters on log only ones that can be reading at a time. 327 00:44:01.590 --> 00:44:06.690 William Cheng: Okay. So, therefore, if you have multiple threads over here, they're reading in parallel or clearly, you have to meet at ah 328 00:44:07.290 --> 00:44:18.510 William Cheng: OK, so again between these two guard commands over here outside regard to command the new tax law. Okay. So, therefore, what you put on the writing over here you also perform the right without the Dimitri lot 329 00:44:20.190 --> 00:44:27.510 William Cheng: All right, very important to understand that. Okay. We have one the right over here. You can take your time and the meetings on blog, you know, so therefore you can take your time doing that, that 330 00:44:28.890 --> 00:44:39.960 William Cheng: Alright, so let's let's just convert them are directly. Okay, so, so we here. I'm going to sort of claim that the code that you see over your CFO is the direct translation from the, you know, from from 331 00:44:40.620 --> 00:44:47.910 William Cheng: From the pseudo code there. Alright, so I'll be here. There's four blocks of code over here. There's one, blah, blah, blah. That right so this one is going to be the implementation of the garden command. 332 00:44:48.150 --> 00:44:57.120 William Cheng: And they'll be here. Here's another implementation of the garden command with the guard and then the bottom over here is going to be another interpretation of the garden command and then another employees, you gotta command. 333 00:44:57.510 --> 00:45:08.190 William Cheng: Okay, so let's try to convert them right and see what happens over here. Okay, the first piece of code over here says when number right is equal to zero readers plus plus. Right, so get howdy cover that. 334 00:45:08.610 --> 00:45:11.430 William Cheng: P three new tax law one Nagar P three condition way. 335 00:45:11.940 --> 00:45:26.010 William Cheng: Okay, so in this case the guard is like this. Right. So while Nagar I'm going to pull exclamation mark mark in front of it. That's my wall Nagar. So if you look at the secret over here P three new tax law. Well, not the guard. Exactly. And then Peter condition. Wait. 336 00:45:26.880 --> 00:45:34.470 William Cheng: Okay, so in this case the piece of furniture way, you need to specify a condition variable to condition variable over here is going to be the readers queue, because the reader threat want to sleep in the readers cute. 337 00:45:36.570 --> 00:45:40.590 William Cheng: Okay, so that that's all you have to do right you convert this code over here. Okay. 338 00:45:41.070 --> 00:45:51.780 William Cheng: piece of meat AX that one RP third condition way. And then if you jump out an infinite loop you execute a command sequence of here's reader, plus, plus. OK. We replied classes right here and then piece of meat on law. 339 00:45:53.130 --> 00:45:55.920 William Cheng: Okay. Very, very simple. That's how you convert that code into sequel. 340 00:45:56.700 --> 00:46:03.690 William Cheng: OK. So again, there is no curly bracket over here for the fault was there, man. You got to make sure that that's the case. Okay, so one Nagar Peter condition way. Okay. 341 00:46:04.560 --> 00:46:09.900 William Cheng: All right. The bottom code over here you know readers, plus, plus, right. So piece to add new tax law readers. 342 00:46:10.230 --> 00:46:20.310 William Cheng: Readers minus minus and then piece of meat on law and also right before you need to check if the number of readers become zero is equal to zero, then in that case you need to wake up a writer sweat. 343 00:46:21.150 --> 00:46:23.940 William Cheng: Okay, so therefore, again, the translation of yours. Very, very simple. 344 00:46:24.180 --> 00:46:34.500 William Cheng: Right piece of meat AX log your document, a number of readers and you do predicament over here, right. So you said minus minus reader over here for us. And then afterwards you check to see if is equal to zero. 345 00:46:35.070 --> 00:46:44.400 William Cheng: Okay, so if after you document number readers even go to zero, that means that you should wake up a writer threat. So in this case, you're going to copy. So I condition signal because you only want to wake up one day. 346 00:46:44.610 --> 00:46:55.050 William Cheng: And in this case, you've got to go to the writers queue, because that's where the writers on the writer threats are sleeping. Okay. So in this case you will wake up one writer thread and then we're done. You call a piece amateur a lot 347 00:46:56.340 --> 00:47:01.830 William Cheng: OK. So, again, very, very straightforward. Okay, what about the third guard command over here, right. So again, 348 00:47:02.040 --> 00:47:10.620 William Cheng: Peace to our new tax law. One night guard pizza condition way so the guard is the entire parentheses over here. So can you put exclamation mark right in front of it. That's your one Nagar 349 00:47:11.250 --> 00:47:23.700 William Cheng: OK. So, again, very, very simple. Yeah, P thread new tax law. Well, not exactly the guard over here. Peter condition way. And now the pizza condition way over here. The argument is that this is the writer threat. So it needs to sleep in the writers few 350 00:47:24.300 --> 00:47:32.460 William Cheng: Guys over here. When is the writers queue instead of the readers few over here. Okay, if it turns out the God the god is true, what do you do right the garden show you execute a command sequence. 351 00:47:32.670 --> 00:47:39.750 William Cheng: The command sequence is simply writers plus plus. Right, so therefore you copy the writers plus plus right here. And then you're dying copies of his alma 352 00:47:41.040 --> 00:47:45.690 William Cheng: OK. So, again, very straightforward. You convert your garden command over here directly into SEQUEL. YEAH. 353 00:47:46.920 --> 00:47:54.180 William Cheng: Alright, the last one over here is writers, minus, minus, right. So again, you do piece me tax law document the writers and you know that the number is equal to zero. 354 00:47:54.330 --> 00:47:58.290 William Cheng: So in this case, you're going to wake up all the readers. Today you're gonna wake up, only one of the writers were 355 00:47:58.590 --> 00:48:08.430 William Cheng: Okay, and then you copy them into a lot. So again, very straightforward piece for me tax lock writers, minus, minus, and you know that the number writers will go to zero because you understand your own code now. 356 00:48:08.760 --> 00:48:16.500 William Cheng: OK, and now you copies or condition signal to wake up one writers. Right, right. So you go to the writers to that's where all the writer third are sleepy, you wake up one of them. 357 00:48:16.740 --> 00:48:27.750 William Cheng: And you call Peter condition broadcast and wake up all the reader sweats. Okay. And then you unlock the meetings over here, guys. So in this example, again, we use one new tasks and we use to condition variables over here. 358 00:48:30.000 --> 00:48:39.030 William Cheng: Alright, so again, he didn't get again. You need to get good at this, right, whenever you see a garden command, you can easily convert them into peace record. Okay, so that's why we write these you know God commands. Okay. 359 00:48:40.950 --> 00:48:48.810 William Cheng: All right, so I think this is a good place to break. So next time we're going to sort of continue to finish the readers and writers problem. And we're going to look at 360 00:48:49.950 --> 00:48:58.620 William Cheng: The barrier synchronization. Okay, briefly, and then we're going to start, start looking at, we're looking at something called 361 00:49:03.030 --> 00:49:09.660 William Cheng: I guess we're not looking at deviation will go look at something else before the deviation, right, we're gonna call it a threat safety. I think that's what we're gonna look at. Yeah. 362 00:49:11.220 --> 00:49:15.300 William Cheng: Alright, so I'm going to stop here and then we're gonna come back to the third video. Okay.