WEBVTT 1 00:00:03.120 --> 00:00:11.759 William Cheng: Okay, this is part three of lecture for. So I was saying we're going to show you a couple of examples of how to prevent deadlock. 2 00:00:12.420 --> 00:00:21.240 William Cheng: So again, the key is by looking at these conditions before. If you can prevent any of these conditions from happening that you guarantee that you will never get into their luck. 3 00:00:21.450 --> 00:00:30.030 William Cheng: Okay, so the first we're going to take a look at how to prevent number four from happening, because a number for saying that in the way for graph, if you have a cycle. 4 00:00:30.360 --> 00:00:39.480 William Cheng: Day, then there's a possibility that you will get into it. They're loud so if we can prevent a cycle from forming. Then we are guarantee that will never get into their never get into it a lot. Yeah. 5 00:00:40.050 --> 00:00:46.260 William Cheng: So, the solution is known as the locking hierarchy. So, so, so in this example over here, we're gonna 6 00:00:47.520 --> 00:00:57.660 William Cheng: We're going to assign a level to every new tax. So in this example. So, for example, so. So again, I guess some people always get confused about this. This is a big if you if you can assign 7 00:00:58.080 --> 00:01:06.510 William Cheng: You know your mute hacks into this levels and their way for graph search satisfy certain kind of relationship then you guarantee that you will never get into a deadlock. 8 00:01:06.930 --> 00:01:11.790 William Cheng: Okay what if you're away for graph doesn't, you know, what if you wait for grab. There's no way for you to 9 00:01:12.480 --> 00:01:19.440 William Cheng: To do to do to assign a level so that these conditions are satisfied. Well, in that case, you can use this method to prevent a lot 10 00:01:20.040 --> 00:01:24.270 William Cheng: Okay. So in a way, if you're lucky, you know, then this will work if it turns out that, you know, 11 00:01:24.990 --> 00:01:32.550 William Cheng: That you were foreground LOOK LOOK SOMETHING DIFFERENT what in this method doesn't work right. So let's take a look at logging hierarchy to understand what kind of condition. 12 00:01:32.760 --> 00:01:40.200 William Cheng: That the way for grad need to look like in order for you to prevent a lot now. Alright, so the idea here is that 13 00:01:40.620 --> 00:01:45.930 William Cheng: The first. The first thing you need to do is that you need to assign these these level to all your all your new tasks. 14 00:01:46.140 --> 00:01:53.100 William Cheng: This example we have 20 new tax there. And the rule is that you know you need to look at, you know, how you program access all these new tax is 15 00:01:53.400 --> 00:02:01.110 William Cheng: The rule says that you must not try to lock on mute at a level I if already holding a mute X is equal higher level. Okay, so 16 00:02:01.620 --> 00:02:10.800 William Cheng: So so so let's say that I equal to two, right. So what is, what is this thing over here. You must not try to lock unmute at your level two. If you already holding a new tax at equal higher level. 17 00:02:11.040 --> 00:02:15.960 William Cheng: If you're holding a mute out to level two, three and four. You are not allowed to wait for me to level two. 18 00:02:16.890 --> 00:02:23.850 William Cheng: Okay, so you can just try any value over here and the plug in this equation over here. If it turns out that the shoe is violated. What, then, it doesn't work. That doesn't work. 19 00:02:24.060 --> 00:02:33.090 William Cheng: Okay, if it turns out that your way for graph everything you know every kind of relationship satisfy the still, you know, this particular condition and then you know that this is going to work. Yeah. 20 00:02:33.690 --> 00:02:41.070 William Cheng: Let's take another example. If currently you are holding these two new tax matrix. Number two meters. Number three, over here, what are, what are you allowed to wait for 21 00:02:41.430 --> 00:02:51.120 William Cheng: Okay, so if you're holding meetings. So if you're holding a meeting a level two and level three. Then, according to this rule over here. You can only wait for him. You'd have to level four 22 00:02:51.570 --> 00:03:01.200 William Cheng: Okay, so, so, so in this case if you try to wait for mutants a level one, two, and three. So if you wait for any, any other 15 minutes over here. What then there's the schemas. 23 00:03:02.220 --> 00:03:08.160 William Cheng: It's not gonna work out. Okay. So why does this particular scheme works okay if you only way for a mute has higher 24 00:03:08.550 --> 00:03:14.160 William Cheng: The higher level than you. So then, in this case, all your arrows going to point downwards. Right. So. So all you're over here is 25 00:03:14.490 --> 00:03:20.970 William Cheng: Again, this is your way for grab all your hours are point downwards. If all the arrows always pointing downward. Is it, is it possible to form a cycle. 26 00:03:21.600 --> 00:03:29.280 William Cheng: Okay, so remember a cycle look like this, right, in order for you to have a cycle. You have to either point sideways or you have to if the 27 00:03:29.610 --> 00:03:35.070 William Cheng: Point upwards. Okay. If the only thing you can do is to point downwards. Well, therefore it's impossible for you to have a cycle. 28 00:03:35.940 --> 00:03:43.110 William Cheng: Okay, so this is, again, this is a very, very simple way. Use a higher use a locking hierarchy to prevent a lead that Adela yeah 29 00:03:43.710 --> 00:03:47.190 William Cheng: The example that we saw before, you know, the example that we saw before. 30 00:03:47.610 --> 00:03:57.570 William Cheng: You know, with a deadlock situation we have to dress like this. So we have two new tax like that like this. So one is way is holding new tax number one with me that summer to swear to over here is what 31 00:03:58.200 --> 00:04:03.900 William Cheng: Is holding me text. Number two, are holding the text, number one, is there any way to assign these two new tax using the 32 00:04:04.440 --> 00:04:13.020 William Cheng: Using locking hierarchy, so that this consider the condition that we saw is going to be satisfied. Well, as it turns out, there's no way right because you know if they're waiting for them. 33 00:04:13.620 --> 00:04:18.720 William Cheng: For each other like this. Well, then, in this case, there's no way for you to, to use the logging hierarchy to solve our problem. 34 00:04:19.560 --> 00:04:26.250 William Cheng: Okay, so therefore we have to abandon login hierarchy and look at a different solution. OK. So the solution going to look at 35 00:04:26.880 --> 00:04:30.060 William Cheng: Over here, this is, you know, this is easy. Number two. 36 00:04:30.420 --> 00:04:39.510 William Cheng: Number two, says that, you know, one of the requirement that you have to do is that you have to, you know, when you're waiting for when you're waiting for a new task. You are not allowed to to release the previous new tax. 37 00:04:39.840 --> 00:04:45.240 William Cheng: Okay, which means that if you're allowed to release the previous muta x, then in that case, you might not get into a dialogue. 38 00:04:45.960 --> 00:04:53.520 William Cheng: Okay, so over here. What does it look at that solution and to see if there's any way we can implement our co so that we can release the meantime while we're waiting for another new tax. Okay. 39 00:04:54.030 --> 00:05:01.110 William Cheng: The idea of here's a procedure number one over here for third one that code doesn't change procedure. Number two, or three, or 432 we're going to change the code. Little bit. 40 00:05:01.500 --> 00:05:10.440 William Cheng: Okay, so what we're going to do is that, okay. So remember, you know, 4321 it will lock me text number one first and then it will lock me text. Number two, and then, you know, in that case will work. All these objects. Okay. 41 00:05:10.830 --> 00:05:15.630 William Cheng: For threat to over here, what we're gonna do is we're going to lock me text. Number two first right so 42 00:05:16.110 --> 00:05:22.650 William Cheng: That that's one of the requirements to actually has libidos number to over here but but what it will do is that it will not try to use object number two right away. 43 00:05:23.100 --> 00:05:29.760 William Cheng: Okay, what do we do that it would allow me, that's number two. And then he will lock meatheads number one immediately to see can actually get me a text number one. 44 00:05:30.450 --> 00:05:37.500 William Cheng: Okay, so in that case, we're going to talk to us a new function. This new function is called piece to me text. Try law, he will try to lock meetups number one. 45 00:05:38.160 --> 00:05:46.080 William Cheng: Okay, so the difference between this function and Peter new tax law is that this function is not a blocking call this function is guaranteed to return immediately. 46 00:05:46.500 --> 00:05:56.040 William Cheng: Yeah. And what do we do it is that if it's able to lock the new tax. What it will do is it will return the value of success if it turns out it was, it's not able to lock them. You guys will actually return right away with the error code. 47 00:05:56.490 --> 00:06:05.130 William Cheng: That if it turns on your success or guess success in a lot of the pizza library cards or equal to zero. So, if we return the value equal to zero, that if not zero, that will be equal to true. 48 00:06:05.310 --> 00:06:09.810 William Cheng: Then you break out of the infinite loop over here. And then at this point you own both me tax. 49 00:06:10.500 --> 00:06:18.840 William Cheng: Okay. So in this case, you're on both meet. So therefore, when you do that you use magic number two first and then you will use object number one and two together. So in this case, you're using object number one and two together. 50 00:06:19.500 --> 00:06:30.000 William Cheng: Okay. And then when you're done, you're like all the new tax okay if it turns out when you try to allow me to number one, number two, number one is not available in this case what you would do is that, here's the key you will unlock new tax number two. 51 00:06:31.140 --> 00:06:34.050 William Cheng: Okay, so at this point will you allow me to ask me to then you don't own any me 52 00:06:34.530 --> 00:06:41.640 William Cheng: At this point, you don't you don't own any meat ax. So therefore, another trek and actually be able to use both me tags and then this way you can avoid a lot 53 00:06:42.000 --> 00:06:49.110 William Cheng: Yeah. So what I will do is I will unlock the text. Number two, and then I will come to the top of the infinite loop. And the next thing that will do is I will try to lock me next. Number two, right away. 54 00:06:49.680 --> 00:06:54.390 William Cheng: Okay, so again the current seem kind of silly you unlock new taxes in between, like that. Number two, right away. 55 00:06:54.780 --> 00:07:00.060 William Cheng: But this is necessary because when you allow me to us number to over here for a while, will be able to obtain the clock. 56 00:07:00.480 --> 00:07:10.710 William Cheng: Will be will be able to obtain the locker room number two. And so I will come execute code over here. Once one is executing code over here was to it will not be able to the whole enemy attacks. 57 00:07:11.370 --> 00:07:17.220 William Cheng: Okay, so therefore, in this case, when so that two copies and meet at Sotheby's X number to over here. So it was gonna get block. 58 00:07:17.550 --> 00:07:27.660 William Cheng: Okay, so therefore this loop doesn't really execute whatever. So what happened is that threat to over here. You know what the matrix is logged over here. So, therefore, what it will do is that the third to actually get block right here. 59 00:07:28.320 --> 00:07:38.460 William Cheng: Yeah, so when it's blah, so I will continue to execute use object number number number one and two together well object number two is done what he would do is they don't unlock both me tasks. So now I will leave 60 00:07:38.730 --> 00:07:49.530 William Cheng: You know, leave the call right here and now, so it will proceed and now you tell them what become available. So, so what is there to over here also like meetups number one, and he would jump out of the infinite loop and you're working object number one and two together. 61 00:07:51.060 --> 00:07:59.250 William Cheng: Okay, so this solution use condition to to make sure the condition to can never happen. So therefore, they will avoid deadlock. So what is the what is the problem with this solution. 62 00:07:59.730 --> 00:08:06.480 William Cheng: Okay, you can see that in the previous in the previous case when we get into the della the code between throw it in threat to their symmetrical. 63 00:08:06.930 --> 00:08:16.110 William Cheng: OK, now the coders us the magical because we didn't change the code for a while, but we modify the code which will try to answer to that is going to give out both new tax even, even though 64 00:08:16.560 --> 00:08:26.550 William Cheng: Even though he has obtained imitrex number two already. Okay, so it looks like thread number to over here has lower priority than fair. Why because, so it doesn't have to wait because it has to give up new tax. 65 00:08:27.630 --> 00:08:38.310 William Cheng: Okay, so you just seen an example over here is that we give these two threads an apparent some kind of a priority to sector, what is more important the threat to without using something called peace to our party. 66 00:08:39.120 --> 00:08:45.480 William Cheng: That. So one thing I want you guys to, to, to avoid doing is that if you search the Internet, you know, some people say, oh, you know, if the piece where 67 00:08:45.870 --> 00:09:00.030 William Cheng: Your thread on executing the right order. What you can do is that you can actually give all these third party to get one through higher priority that and another threat a lower priority. So this way. The hard part is I will run first and then a lower priority will will 68 00:09:00.750 --> 00:09:04.110 William Cheng: Will run over and we'll run a later time. 69 00:09:04.920 --> 00:09:15.180 William Cheng: Okay, I want you to resist temptation to write code like that. Okay, you need to write your code so that also adds have equal priority, and then you still you there's a way for you to get the code work. 70 00:09:15.870 --> 00:09:21.600 William Cheng: Okay, that will be the wrong way to do it. Okay. The problem with the piece of property is that pizza parties actually kind of events. 71 00:09:22.020 --> 00:09:28.500 William Cheng: Sort of the topic. If you try to use piece of property. If you are not very, very careful, you're going to end up, you know, something will never get to run 72 00:09:29.400 --> 00:09:38.730 William Cheng: Okay, so you something I'd never good runs, kind of like a lot but again it's not a deadlock because another thing I should only has higher priority. So therefore, the others are all just at good run, and you'll never guess around 73 00:09:39.660 --> 00:09:49.200 William Cheng: Okay, so when you're doing your warm up to, if you try to control the order of your threats, you know, this is the I want this way to execute first and that there's execute to next either just don't do that. 74 00:09:49.680 --> 00:09:59.160 William Cheng: Okay, or doing it a different way. Okay, so, so for for one or two at there's really no requirement to to to to sort of control the execution order of your threat. 75 00:09:59.670 --> 00:10:07.170 William Cheng: Now he has it again. Don't use pizza party right, whereas in this class. We're not gonna even talk about pizza party because it's really bad idea. Yeah. 76 00:10:08.970 --> 00:10:16.950 William Cheng: Alright, so we're going to summarize mutation, which has a very simple piece of your desktop, you said a lot, right, so, so, so, so I'm going to ask a question of how to 77 00:10:17.310 --> 00:10:21.030 William Cheng: interact with each other, you create all these threads register is going to interact with each other. 78 00:10:21.240 --> 00:10:29.730 William Cheng: There are two ways that can interact with each other. Number one is that they have, they can use one of the pieces of function calls right, will you use peacetime the tax law you have one thread wait for another thread. 79 00:10:30.720 --> 00:10:39.660 William Cheng: For the altar to release the new tags. So that's one way you know to 200 K truck which with each other. So you can use these pieces library calls pizza pizza pizza me a lot. 80 00:10:40.170 --> 00:10:43.530 William Cheng: The other way for them to interact with each other is to use share variables. 81 00:10:43.980 --> 00:10:50.100 William Cheng: Guys will get the shareable can be any wearable inside your address space as long as you have the address. You get to share these variable, okay. 82 00:10:50.340 --> 00:10:59.220 William Cheng: So in this case, they, you know, so you see the example that we saw before his ex wife both that wants to use x. So x is how these two threads sort of interact with each other. 83 00:11:00.360 --> 00:11:07.290 William Cheng: Okay, so it sounds a little weird. Okay. But, but when you have multi threading the way they interact with each other is through these there too so so all these variables. 84 00:11:08.910 --> 00:11:19.380 William Cheng: So let's talk about the share variables over here if multiple threads wants to share read only data. So if a piece of variable up here called ads. Right. You never modify X. All you want to do is look at the value of x. 85 00:11:20.280 --> 00:11:28.080 William Cheng: Okay if multiple threads. All they do is that they promise that that they will only look at us and they will never modify the share data. Well, so in this case, you don't need 86 00:11:28.620 --> 00:11:39.060 William Cheng: You don't need to meet tax at all, right, because if you just want to read as a. You don't want to share it. You don't want to modify it. Well, then in that case, everybody. Good night, you read Acts exactly the same time, and there'll be perfectly fine. 87 00:11:40.470 --> 00:11:48.150 William Cheng: Okay, so, so please understand that you don't have to use monetize if all you want to do is to read if all you ever want to do is to read 88 00:11:48.630 --> 00:11:49.980 William Cheng: Read a particular variable. 89 00:11:50.370 --> 00:11:58.800 William Cheng: That now if multiple threads wants to share data for both reading and writing. Okay, or just want to write in, but if you just want right it's kind of unusual right 90 00:11:58.950 --> 00:12:08.190 William Cheng: Typically what you want to do is that you want to read it. You want to write it. Okay, so if multiple three I want to share data for both the reading and writing it doesn't really matter how often do you read a Harvard do right 91 00:12:08.370 --> 00:12:14.880 William Cheng: At some point, you're going to both read and write, or if one wants to read and once it wants to write. Well, in that case, you got to be very, very careful. 92 00:12:15.150 --> 00:12:21.060 William Cheng: All these threats about show me attacks. Right. So yeah, you got to put all these variables inside to see reservation box. 93 00:12:21.240 --> 00:12:26.580 William Cheng: And the decision box has a new text in front of it and then this way, you call piece of your time to get into the serialization box. 94 00:12:26.760 --> 00:12:34.140 William Cheng: Once you're inside of the reservation box you execute critical section code and we execute critical section Co. You can read or you can write a variable ex. 95 00:12:35.550 --> 00:12:47.220 William Cheng: Or is it again, these, these rules are very, very important if you violate these rules. Your codes not gonna work. Or even worse is that your code is going to work sometimes and sometimes it doesn't work. Okay. That will make debugging very, very difficult. Yeah. 96 00:12:48.600 --> 00:12:58.290 William Cheng: Alright, so here's a comment that said, in general, critical section code may be nested. So when we look at the lock hierarchy, right, the code that we just saw before. 97 00:12:58.920 --> 00:13:07.200 William Cheng: This is the case where the critical section called up here is nested right you locked me attacks number one over here. So let's say that you love me text number one over here you release me like number two. 98 00:13:07.440 --> 00:13:16.740 William Cheng: Number one, right here. So the, the code over here between new tax law can meet Allah. Allah for me. It has done, but why is critical section called with respect to meet X number one. 99 00:13:17.160 --> 00:13:27.330 William Cheng: Okay, but instead of critical section over here because the call over here. You love me text. Number two, and then you allow me text me too. So the code in between over here is critical section called with respect to the text. Number two. 100 00:13:27.600 --> 00:13:32.250 William Cheng: Okay, so the inner critical section code is actually inside the outer cargo section code. 101 00:13:33.420 --> 00:13:38.370 William Cheng: Alright, so again, this is the all you need to understand that this can actually happen. So when you use lucky hierarchy. 102 00:13:39.000 --> 00:13:47.940 William Cheng: You know, this kind of stuff, you know, or at least one word when we try to use lucky hierarchy, you're going to end up with one critical section nested inside another critical section. Okay. 103 00:13:48.720 --> 00:13:55.860 William Cheng: Right, so that could certainly have that and also a threat may be using different new tax in different new taxes to interact with different threat. 104 00:13:56.490 --> 00:14:01.230 William Cheng: OK. So again, it's very, very important when you talk about a critical section, the critical surgeons with respect to one new tax. 105 00:14:01.380 --> 00:14:08.850 William Cheng: So will you know so for for one threat. Sometimes you want to interact with threat. A and something you want to interact with three be authentic, there'd be a completely separate 106 00:14:08.940 --> 00:14:16.530 William Cheng: So maybe you have multiple tags. So sometimes you have to be taxed number one log and sometimes you have any tax number to LA and maybe a new text. Number one, you have a variable x and 107 00:14:16.830 --> 00:14:23.040 William Cheng: X number to over here you have variable. Why so. So again, a threat can interact with multiple threads at different time and that's perfectly okay 108 00:14:23.520 --> 00:14:29.250 William Cheng: Okay, so when you are doing your warm up to exercise that you need to sort of think about all these kind of stuff these on your program. So to figure out 109 00:14:29.610 --> 00:14:41.430 William Cheng: When she threats interact and then in this case you need a new tax, you know, and also, you know, so you can share variable and either figure out, oh, you know what they are and also follow all these rules. Otherwise, your programs, not gonna work. Yeah. 110 00:14:42.960 --> 00:14:52.770 William Cheng: Alright, so we are done with the basics me tax is very simple new tax law because unlock so unfortunately it's ugly. It's really not general enough. We're also going to take a look at some other 111 00:14:53.400 --> 00:15:01.470 William Cheng: To the, the other synchronization primitives. Yeah. Alright, so this is called Beyond new Texas. Right. So we talked about meetings already is very, very simple. 112 00:15:01.710 --> 00:15:13.020 William Cheng: New Taxes necessary when share data has been modified so that's just what I mentioned over here if you want to modify something you have to use the new tax. Okay, although there are cases where using a new tax is an overkill. 113 00:15:14.070 --> 00:15:17.790 William Cheng: Okay, so what does an overkill overkill. That means that you know you're using something too powerful. 114 00:15:18.180 --> 00:15:29.640 William Cheng: For something that's, you know, that doesn't really require to you something very, very powerful. So in this guy's overkill in this case is that using the new tax is too restrictive and efficient that will lock the threats out when it's not necessary. 115 00:15:30.540 --> 00:15:36.630 William Cheng: Okay, so what, so. So in this case, why would you lock a threat are unnecessarily. Right. So for example, if you have a big data structure that you're sharing 116 00:15:37.290 --> 00:15:42.960 William Cheng: Okay. What if one thing I just want to read the left side of the data structure. And the other thing I want to read the right side of 117 00:15:44.160 --> 00:15:48.510 William Cheng: The. The other thing I want to write want to modify the right side of the data structure. 118 00:15:49.740 --> 00:16:02.790 William Cheng: Okay, so again, once I want to read the left side of the data structure and the other the other. The other thing I want to write to the right set of data structure. Well, in that case, if you use the new tax lock this data. Okay. That will be overkill because you're losing parallelism. 119 00:16:04.230 --> 00:16:12.660 William Cheng: Okay, this thread wants to read the left side, this or I want to write to the right, that what they are modifying different parts of the data structure. Why do they, you know, why does the music has to be locked 120 00:16:13.860 --> 00:16:20.760 William Cheng: Okay. So, therefore, if you use the new tax, even though using the new test will be correct. But in this case, you are being a little waist well because you lose power was up. 121 00:16:21.300 --> 00:16:27.810 William Cheng: Okay. The reason we want to run multi threading, is that we want to have as much power as as possible. So sometimes if you or if you just use the new tax. 122 00:16:28.500 --> 00:16:32.580 William Cheng: To to lock up everything or that'd be now might be very, very use that may be very wasteful. 123 00:16:32.850 --> 00:16:43.440 William Cheng: Okay. So for example, you have a complicated program. Right. The example that we saw before, there's there's 20 new tags over here. So instead of 20 new tights. What we can do that, we can actually replace a 20 minute guys both one giant me attacks. 124 00:16:43.650 --> 00:16:51.030 William Cheng: And when you try to access any data that's being shared by any threat you lock a giant attacks and now you can work on the data or you want it. When you're done, you have to be attacks. 125 00:16:52.020 --> 00:17:04.200 William Cheng: Okay, so in that case, again, it's going to be correct, right, because when you are when you have the new tax law, you get an exclusive use of your data. So therefore, you're not going to step on any other threat the, you know, you're not going to step on any other threat. 126 00:17:04.530 --> 00:17:10.860 William Cheng: Rabbit. In that case, it could be very, very inefficient right because because you're going to lose any kind of a parallelism that potentially you can have 127 00:17:11.850 --> 00:17:16.710 William Cheng: Okay, so therefore we need to write code, not only correct, but also in but also efficient. 128 00:17:17.130 --> 00:17:24.690 William Cheng: Okay, so that's what we're going to talk about over here yet or we talk about beyond your tax. OK. So again, it's always correct to just use the tax law have everything 129 00:17:25.110 --> 00:17:35.970 William Cheng: There but that's not very efficient. Okay. So, therefore, now we know, we know how to use a meat ax to do things correctly. Now, we also need to learn some other tricks in order for us to use the tags efficiently. Yeah. 130 00:17:37.320 --> 00:17:39.330 William Cheng: Alright so here you know 131 00:17:40.080 --> 00:17:49.710 William Cheng: The main problem is going to have no parallelism. So we would like to have better concurrency. Right. We don't want to have fine grained problems. And so we can actually control powers and if you want to read the website and other thing. 132 00:17:50.130 --> 00:17:52.470 William Cheng: You know, right, to the right side. Maybe they can do this in parallel. 133 00:17:53.610 --> 00:17:57.930 William Cheng: Yeah. So in this case, complete the mutual exclusion is not required. There are 134 00:17:58.440 --> 00:18:07.380 William Cheng: Two major category that we're going to look at number one is that, is that what if it doesn't interfere with each other. Most of the time, and the synchronization is only required occasionally 135 00:18:07.710 --> 00:18:10.560 William Cheng: Okay, so like this example I just showed over here. Okay. 136 00:18:11.040 --> 00:18:21.330 William Cheng: We are modifying the same data structure, but we're modifying different parts of the data structure. So, therefore, you know, when we modified different parts of the data structure, you know, in this case, you know, they don't really interfere with each other. 137 00:18:21.750 --> 00:18:26.940 William Cheng: Okay. It just happened. They use the same data structure, but since they are using different parts of it they don't really interfere with each other. 138 00:18:27.240 --> 00:18:31.140 William Cheng: Okay, so one generic problem. We got to look at. It's called a producer consumer problem. 139 00:18:31.380 --> 00:18:35.190 William Cheng: Okay, so for example you have some, you know, some kind of a data structure. 140 00:18:35.370 --> 00:18:44.370 William Cheng: One thread is producing data on the left side. The other ones consuming data on the right hand side over here. Well, in that case, they shouldn't interfere with each other. Right. So that will be the first example we're going to look at. Okay. 141 00:18:45.210 --> 00:18:49.470 William Cheng: This is also known as the bounded buffer problem. And we're going to, sort of, you know, give you an introduction to what that is. 142 00:18:50.280 --> 00:18:54.450 William Cheng: The second case over here is what is something I just want to look at a piece of data. 143 00:18:55.380 --> 00:19:01.380 William Cheng: Okay, so in that case, if you, you know, so, so, so let's say that you have, you know, three wants to read data and you have third at once the data. 144 00:19:01.800 --> 00:19:04.230 William Cheng: But the third wants to write data didn't really occur very, very often. 145 00:19:05.100 --> 00:19:10.200 William Cheng: Okay, so, most, most of the time you have a bunch of stuff they want they want. They all want to read the same data. 146 00:19:10.530 --> 00:19:21.810 William Cheng: Okay, so in this case the writer hasn't shown up yet. So this is known as the readers and writers problem. You got to have a lot of readers are you gonna have a lot of writers in the writers doesn't show up wanting that case all the readers should be able to read in parallel. 147 00:19:22.860 --> 00:19:32.670 William Cheng: Okay. As soon as a writer comes in, while then the writer wants to modify the data exclusively. So in that case, you know, want to make sure that know readers are reading and no other writers are writing 148 00:19:33.690 --> 00:19:41.550 William Cheng: Okay, so, so again, what's important to understand over here is that if there is no writer all the reader should be able to read in parallel. So if you just use the new tax you can't really do that. 149 00:19:41.850 --> 00:19:52.680 William Cheng: Okay, so, so we're going to talk about what solution for this to be correct and also to be efficient. Yeah. There's also something called a barrier synchronization. We also want to look at. By the end of this, you know, the other set of slides. 150 00:19:53.880 --> 00:20:01.560 William Cheng: Alright, so let's first talk about the producer consumer problem. So the idea of years is that conveyor belt. Right, so it is conveyor belts and mechanical devices over here. 151 00:20:01.740 --> 00:20:07.350 William Cheng: We're going to have the producer, the produce data on the conveyor belt and then the conveyor belt is going to move to the left over here. 152 00:20:07.500 --> 00:20:15.540 William Cheng: And the consumer over here is going to pick up that data over here. And then what it would do that. It will take it will modify it maybe put it back on the conveyor belt at a later point 153 00:20:15.990 --> 00:20:23.820 William Cheng: That part doesn't really matter. Okay, what's important to understand over here is that for this conveyor belt can to produce produce at the same time as consumer consumer 154 00:20:24.450 --> 00:20:30.210 William Cheng: Right, so the answer, of course, is yes, you can put a piece of data right here, while the consumer over here. Take a piece of the 155 00:20:31.080 --> 00:20:38.580 William Cheng: Cake or something over here to work on it. Okay, so most of the time, producing consumer over here, they will actually never work on the same spot. 156 00:20:39.060 --> 00:20:43.860 William Cheng: I mean, clearly this is the case for a factory, right, because these these these things are mechanical so they're 157 00:20:44.220 --> 00:20:50.460 William Cheng: They're sort of part of the sort of hardware that are working in lockstep so therefore they can never work on the same slot. 158 00:20:51.060 --> 00:20:54.150 William Cheng: Okay, but if the producer consumer over here. They are software. 159 00:20:54.360 --> 00:20:59.280 William Cheng: That it is possible. One of them is going to run faster than the other one. Right. So for example, if the consumer is very, very fast. 160 00:20:59.460 --> 00:21:07.710 William Cheng: You will work on the data over here, they will go to the next piece of data, it will cause the next piece of data, pretty soon the consumer will be trying to take out data that a producer trying to produce 161 00:21:08.580 --> 00:21:18.840 William Cheng: That's in this case, who should wait while the consumer has to wait because the producer. Hasn't produced finished producing data into, you know, this part of the conveyor belt. Yeah. So therefore, the consumer has to has to wait. 162 00:21:19.710 --> 00:21:26.310 William Cheng: Okay, what about the producers going too fast, right. So in this case, you know, the conveyor belt. So if you think about a conveyor belt. It's actually a circular data structure. 163 00:21:26.520 --> 00:21:31.260 William Cheng: Just like the W link circular list rather you to think about, you know, the end though because I should wrap around 164 00:21:31.440 --> 00:21:40.470 William Cheng: So if my producers too fast, what's going to happen, right. I'm going to produce data here, here, here, here, here, here, here, and when I reached the end over here. I'm going to start producing data over here. 165 00:21:40.650 --> 00:21:49.920 William Cheng: If the consumer is really, really slow. When to produce a get to where the consumer is taking data out. Now, the producer has to wait because if if we produce data right here is going to destroy this data. 166 00:21:50.280 --> 00:21:57.060 William Cheng: Okay, so yeah. At that point, if you allow the producer continue to produce it will step on each other's toes, you will step on the toes of the consumer. 167 00:21:58.110 --> 00:22:03.330 William Cheng: Okay. So in this example, you can sort of see that if the producer and consumer for some reason they're running in lockstep 168 00:22:03.540 --> 00:22:14.190 William Cheng: Okay, they're working on different spot of the conveyor belt of it so. So again, what is the conveyor belt. Read the conveyor belts. My forward to this guy. So in this case, what you will do is that you will, you know, add data, you know, you're, I guess, you know, 169 00:22:15.120 --> 00:22:20.730 William Cheng: To insert something like that. Well, in this case the data structure is pre alligator. You're going to take one of the object over here you have a point to a 170 00:22:21.360 --> 00:22:24.390 William Cheng: New object and the other case over here. You're going to take the object out 171 00:22:25.140 --> 00:22:32.070 William Cheng: Okay, so in this case again over here, the producer over here is a piece of software. So therefore you don't know you know he can run faster or slower, but he's 172 00:22:32.790 --> 00:22:40.500 William Cheng: Been the case. I'm very, very lucky to producer and consumer are producing and consuming a exactly the same rate. Well, in that case, do I really need a new tax. 173 00:22:41.250 --> 00:22:48.600 William Cheng: Okay, because if they are working on different spots all the time. And they're moving down the conveyor belt. Well, in that case, I would never have to use a new tasks. 174 00:22:49.080 --> 00:23:00.870 William Cheng: Okay, but since I can't really control it, because sometimes they will run faster than the other thread because they execute. Let's call it a more code. I don't really know. So it is possible that will be working in the same spot. So therefore, occasionally I need synchronization. 175 00:23:02.010 --> 00:23:07.500 William Cheng: Okay. All right. So, so one solution over here is to use the new tax to lock up the entire data structure. 176 00:23:07.830 --> 00:23:15.480 William Cheng: Okay. So in this case, when a producer want to produce something, it will lock the view tax put data in their anatomy tax and when the consumer wants to consume something 177 00:23:15.660 --> 00:23:22.830 William Cheng: It will lock the data structure over here, the entire data structure take something out over here and I'll finish data structure. Okay, so if you take that approach. 178 00:23:23.220 --> 00:23:32.910 William Cheng: If you take that approach. He will be a correct solution but you lose all the powers that because in that case there will be no way for the producer consumer to be producing and consuming at exactly the same time. 179 00:23:33.750 --> 00:23:41.580 William Cheng: Okay, so we want a solution that we can, that is that is logically correct that they would never step on each other's toes. Also, we want as much 180 00:23:42.810 --> 00:23:52.290 William Cheng: As possible. Okay. All right. So what is the solution. So in order for us to introduce the solution, we're going to introduce a new programming language. Verse 181 00:23:52.650 --> 00:24:03.300 William Cheng: I just getting over here. Okay, so, so this is a sort of a programming language for pseudo code. Okay. It's called Garda command. This one was invented by Dykstra, you probably heard of thanks, you know, is distributed 182 00:24:03.660 --> 00:24:10.140 William Cheng: Computing person he invented a lot of stuff in the 70s i one of them is the garden commit right so we're going to first introduce Dr command. 183 00:24:10.440 --> 00:24:15.360 William Cheng: And then we're going to use the Garter command to implement this producing customer problem. Okay. 184 00:24:15.900 --> 00:24:27.990 William Cheng: All right, so some people when they see the slide. They gonna ask me a question, you know, is this going to be on the exam. Okay, so let me be very, very clear. For example, you need to know how to write simple pseudo code in the language of God commands. 185 00:24:28.500 --> 00:24:35.490 William Cheng: Okay, what is the symbol pseudo code in the language of God commands. So here's the definition of what a garter command look like you have to use the keyword when 186 00:24:36.060 --> 00:24:42.030 William Cheng: Okay, now you know. Now, while not in for something that you have used the keyboard. When if you don't use when you don't lose points. Okay. 187 00:24:42.300 --> 00:24:49.860 William Cheng: And then there's a condition over here condition is just like the if then else right there's a fallback condition. Wow, followed by the condition is over here. It's called guard. 188 00:24:50.130 --> 00:24:54.540 William Cheng: Okay, so God. It's a simple you know expression or that will give you, true or false. And nothing in between. 189 00:24:54.990 --> 00:25:01.500 William Cheng: Okay, so this one says when the guard is true and then followed by the square bracket not curly bracket now parenthesis per square bracket. 190 00:25:01.800 --> 00:25:09.450 William Cheng: Yeah. So in between the square bracket over here. This is not a command this know as the command sequence sequence is just a bunch of code or pseudo code. Are you gonna right 191 00:25:10.470 --> 00:25:24.480 William Cheng: Okay, so when you re, you know, what will you write your pseudo code using the language of the garden command. So this is what it means. So this means that this the command sequence is executed automatically when the guard is evaluated to be true. 192 00:25:26.250 --> 00:25:36.570 William Cheng: OK. So again, this is written in a very precise language. Right. The guard the command sequence, which means that the statement in front of in the middle over here that's commented out is executed in one atomic execute 193 00:25:38.040 --> 00:25:42.720 William Cheng: In one atomic operation. Okay. When the guard is value to be true. 194 00:25:44.190 --> 00:25:51.510 William Cheng: OK, so the guard over here rise how. So how do you evaluate the car to be true. Right. So some thread must be executing code inside of CPU, because otherwise how do you evaluate the guard. 195 00:25:52.170 --> 00:26:00.900 William Cheng: OK, so the guard. Again, it's a it's a Boolean expression can be evaluated, true or false. So at some point in order for you to evaluate a dog. You have to be running out of CPU. 196 00:26:01.080 --> 00:26:14.700 William Cheng: If your thread is sleeping well then you have no idea you because you have no way to execute evaluate the guard. Yeah, and also the says that when the goddess value to be true. One of the guard over here is false. Okay, when you evaluate the garden of God is false, nothing happens. 197 00:26:16.080 --> 00:26:20.910 William Cheng: Right. But when you evaluate the guard to be true. You have to execute a command sequence in one atomic operation. 198 00:26:21.240 --> 00:26:33.240 William Cheng: Okay, alright. So, over here is a common are not automatically means that is executed without interruption there. But as I mentioned before, when you talk about the atomic operation, you have to say, what is it, what is it with respect to 199 00:26:33.900 --> 00:26:44.910 William Cheng: Okay, so they don't want us to be be clear. So in the end, you know, the mystery over here is that it's going to be a mute Texas getting involved over here. So therefore, the atomic operation over here is going to be with respect to the operation of that new tax. 200 00:26:45.360 --> 00:26:52.590 William Cheng: Okay, so, so right now. Right now we're seeing a pseudo pseudo code. So therefore, there's no new tax and later on we're going to introduce a new tasks into the pseudo code now. 201 00:26:53.340 --> 00:27:02.280 William Cheng: Alright, so what's important to understand over here is that evaluating the guard to be true and executing the command sequence all together. It's an atomic operation. 202 00:27:02.520 --> 00:27:09.840 William Cheng: Okay. It's not that the command sequence itself is the atomic operation is the command sequence itself plus seeing the guard to be true. 203 00:27:10.680 --> 00:27:19.380 William Cheng: Okay, so when you see the guard to be true. You are beginning your atomic operation already if the goddess show you execute a command sequence that's all locked together into one atomic operation. 204 00:27:20.250 --> 00:27:30.990 William Cheng: Okay. Alright, so let's take a look at, you know, pictorial what it looks like. So again, when God you know his command sequence. Right. So what happened is that, you know, the guard over here. The guard can be actually very, very complicated. 205 00:27:31.770 --> 00:27:42.270 William Cheng: Okay. The guard over here can be you know this was, let's say that you know when a is bigger than five AND YOU KNOW, FO b is greater than equal to see 206 00:27:43.170 --> 00:27:49.170 William Cheng: Okay, so this could be my god. Nice. Okay, this is a Boolean expression and it can be true or false, right, so what's tricky about this is that 207 00:27:49.590 --> 00:28:01.860 William Cheng: A, B, and C over here, there are variables, they are being changed by three different threads in three different CPU simultaneously while you are the fourth were running the four CPU tried to evaluate this this particular guard. 208 00:28:03.750 --> 00:28:11.430 William Cheng: OK, so again you are sitting in one CPU over here you are trying to access a, b, and c, a, b, and c, they're being changed by three different CPU simultaneously. 209 00:28:11.850 --> 00:28:17.250 William Cheng: Because they are, you know, running different threads inside your same process. So, all these three thread. They're trying to change ABC like crazy. 210 00:28:18.090 --> 00:28:26.460 William Cheng: Okay. So in this case, you're washing the car look like whereas on. Here's a picture of what the guard look like since ABC being changed like crazy over here, horizontal axis is time. 211 00:28:26.910 --> 00:28:34.110 William Cheng: Here's the value of the guard can go from false true to the files going up and down like crazy because they're being modified by three different threads simultaneously. 212 00:28:35.190 --> 00:28:40.830 William Cheng: Okay. So in this case, if I'm the four threads at some point, I need to come in, evaluate a guard right so therefore the picture should be like this. 213 00:28:41.070 --> 00:28:44.760 William Cheng: At some point over here. I need to sample the timeline and try the value of the guard. 214 00:28:45.120 --> 00:28:52.530 William Cheng: Okay, so I need to go to be valid, this expression. So, for example, a time you got a tier one I'm coming evaluated guards guards false. Okay, so when God is false. Nothing. 215 00:28:53.010 --> 00:29:02.550 William Cheng: There's nothing I can do. Right. Because over here says only one God is true. I will do something when the guard is false. I don't do anything that. So, therefore, what I need to do is I need to come, evaluate the guard later on. 216 00:29:02.940 --> 00:29:08.280 William Cheng: Okay. It takes time for you to evaluate the guard right so anytime go to to to accompany value to guard again what God is still falls. 217 00:29:08.550 --> 00:29:14.460 William Cheng: At time you go to the T three.com again, we're pretty close over here, but I still miss it. So in this case, again, there's nothing I can do. 218 00:29:14.850 --> 00:29:23.460 William Cheng: at time equal to T for coming, evaluate the guard and I see that the guard is true. So therefore I must execute a command sequence in one atomic operation. 219 00:29:23.760 --> 00:29:33.900 William Cheng: Okay, so therefore at time t, for I see the guard to be true. I need to immediately execute a command sequence and this needs to be locked together in one at one atomic operation. 220 00:29:34.590 --> 00:29:48.450 William Cheng: That. So, therefore I'm going to draw a pink box over here lock them together in one atomic operation by saying the word atomic at the bottom over here. Okay, so that means that nobody else can interfere with this operation over here with respect to whatever that we're gonna 221 00:29:49.500 --> 00:30:03.300 William Cheng: We're gonna look at later okay alright so so evaluated guard to be true and excellent command sequence over here. It's going to be locked together in one atomic operation. Okay, so, so please understand that the command sequence does not equal to critical section. 222 00:30:04.410 --> 00:30:20.580 William Cheng: Okay, so why is that the case over here, right, because the critical section is, you know, the could associate is the one that surround the atomic operation right you can see that this picture over here, the atomic operation is evaluate the guard to be true and excellent command sequence. 223 00:30:21.750 --> 00:30:26.250 William Cheng: Or so, therefore, you know, the critical section is a little bigger than the command sequence. 224 00:30:27.810 --> 00:30:35.820 William Cheng: All right. Okay. So, evaluate the guard to be true and actually recommend sequence altogether is done in one critical section and in one atomic operation. 225 00:30:36.300 --> 00:30:43.530 William Cheng: Because you can again can see that there's going to be a mute technically involved somewhere and then the atomic operation over here is with respect to them. He attacks. 226 00:30:43.800 --> 00:30:48.750 William Cheng: Okay. All right. And since this is done in one atomic operation. So we can actually combine them into one action. 227 00:30:49.350 --> 00:30:57.120 William Cheng: Okay. And then in this case it will be done in zero time at time t for we're going to evaluate a guard to be true and excellent sequence in zero time 228 00:30:58.050 --> 00:31:05.610 William Cheng: Okay, so in this case the execution duration of the all these things over here we go to zero. Can we do that while we can do that because there's pseudo code. 229 00:31:06.120 --> 00:31:16.050 William Cheng: Okay, even though in reality, we know that a tight. It takes time for you to evaluate God and it takes time to execute commands sequence actually recommend Seguin can take you seconds can take two minutes, doesn't really matter. 230 00:31:16.380 --> 00:31:21.360 William Cheng: Okay, but if a pseudo code does. It is time to go to T for this is when everything happens in an instant of time. 231 00:31:22.920 --> 00:31:31.110 William Cheng: Alright. So again, that's what you should think about when you see an atomic operation. It's as if you know nothing and getting to the middle because of executing one instance of time. Yeah. 232 00:31:33.060 --> 00:31:43.920 William Cheng: Alright, so we're going to use you know Garda command to implement some of course, you probably heard of Santa fours or if you have ever written. Kind of a multi threaded code you have little you know little you have since 74 233 00:31:44.340 --> 00:31:50.880 William Cheng: So let's define 74 so in the same paper Dykstra, you know, talk about Garda command. He also introduced me force. Okay. 234 00:31:51.270 --> 00:32:00.300 William Cheng: So because the reason that he he he introduced God commands it because you want to use Garda command to implement them, of course, because what does that mean for us. Okay, so here's what it is right. 235 00:32:00.630 --> 00:32:12.630 William Cheng: A summer for is just an integer. Okay, so some of what we're going to use the notation. So it's a variable. It's an integer, it's a non negative integer it go from 0123 to a very high number. It can come back to zero, but it can never go negative. 236 00:32:13.620 --> 00:32:24.090 William Cheng: Okay, so a center for. So over here, it's a it's a non negative manager on which there are two basic operation divide divide the seven for using Garda commands. 237 00:32:25.050 --> 00:32:31.860 William Cheng: Okay, so it's the seven four is an abstract data type is going to be defined by using these these function or these operations on a semi for 238 00:32:32.370 --> 00:32:42.090 William Cheng: That one is called the P operation and the other one is called the V operation PM be there are Dutch words because Dykstra is Dutch, so I can't pronounce them. So anyway, so these days. 239 00:32:42.480 --> 00:32:52.920 William Cheng: The P operation over here is known as the atomic document operation and the V operations noise atomic increment operation. OK, so the P operation over here, when they implemented the garden command. It looks like this. 240 00:32:53.550 --> 00:33:02.970 William Cheng: Okay, so this is when x is greater than zero. Okay, so, so when you evaluate a guard S is greater than zero, then you do a sequel to x minus one. 241 00:33:03.420 --> 00:33:08.040 William Cheng: Okay, it's kind of obvious, right, because if x is equal to zero. Well, then in that case, you cannot detriment it 242 00:33:08.940 --> 00:33:18.570 William Cheng: Okay, so therefore, again, you need to write this in terms of a garden command, because the garden command implies that you can actually, you know, you have, you can have many, many threads. Try to execute this particular statement at exactly the same time. 243 00:33:19.440 --> 00:33:30.300 William Cheng: Okay. Alright, so the P operation is the atomic detriment operation. You can only detriment something if x is greater than zero. Right. Okay. The atomic other the operation of yours is called the atomic increment operation. 244 00:33:31.050 --> 00:33:37.050 William Cheng: As you can see that there is no guard so you can do this anytime you want. So in this case, why do we put it inside a garden command. 245 00:33:38.460 --> 00:33:43.860 William Cheng: Okay. The reason we put inside a garden commands over here is that in this case as you can do is equal to x plus one. 246 00:33:44.010 --> 00:33:52.200 William Cheng: And as equally as possible, as we saw before you can combine two three machine instructions. So all the three machine instruction get locked together in one atomic operation. 247 00:33:53.190 --> 00:34:02.430 William Cheng: OK. So again, when you try to implement a V operation. If you leave out the square bracket. Is that correct, no it's incorrect. Right. So the square bracket imply that this is going to be an atomic operation. 248 00:34:03.900 --> 00:34:08.580 William Cheng: Alright, so this is the operational i said before you can do atomic document can do atomic increment. 249 00:34:09.000 --> 00:34:18.990 William Cheng: They are no other means for manipulating the value, the value of the Center for other day initializing it. Okay, so you can initialize to a value, you know, typically bigger than zero. Okay. 250 00:34:19.740 --> 00:34:29.430 William Cheng: All right, let's take a look at how to use them. Of course there. So here's a function called one at a time, over here, guys. On the right hand side, I'm going to remind you that definition or P amp D. 251 00:34:29.880 --> 00:34:39.480 William Cheng: Guys, again, the POV is timing document these the atomic increment operation. We're going to initialize this one before equals two, one there. So now we used to put the function to do one at a time. 252 00:34:39.750 --> 00:34:51.510 William Cheng: If you have 100 threads calling one, the one at a time. Simultaneously, what's going to happen. Okay. If I 100 threads over here there in 100 CPU. They all call one at a time, simultaneously. 253 00:34:52.050 --> 00:35:03.750 William Cheng: Okay, what's going to happen right over here. The code over here says you're going to perform the operation SS equal to one over here. So one of the threads going to see is equal to the equator, the one that there were detriment as from one to zero. 254 00:35:04.950 --> 00:35:13.680 William Cheng: Okay, so one of the 100 side over here, we don't know which one it is. One of them is going to see as equal to one and that one will be the thread that doctrinal one to zero. So that's there will be actually a call right here. 255 00:35:13.980 --> 00:35:19.080 William Cheng: What about all the other 99 threat. Is it possible for another threat to see as also equal to one. 256 00:35:20.340 --> 00:35:24.570 William Cheng: Read the answer is impossible because of the definition of PFS 257 00:35:24.960 --> 00:35:36.030 William Cheng: Okay, whichever thread I see is greater than one, it would it would recommend us to a zero in one atomic operation. So that means that another thread will not be able to see the value of x, the value of mess. 258 00:35:36.390 --> 00:35:45.330 William Cheng: With this atomic operation of your happens. Okay. So, therefore, if you write your code. This way, your guarantee that all the other 99 they all see as has a value of zero. 259 00:35:46.050 --> 00:35:53.610 William Cheng: Okay, so therefore once they will be executing over here, but all the other 99 so that they can continue to execute this code over and over and over again, they will all see as equal to zero. 260 00:35:55.470 --> 00:36:04.770 William Cheng: Okay, so all the other 99 they are they're stuck a piece of ass right so you can again you can sort of think about that as a blocking call. So this guy's all the other 99 thread that are block. 261 00:36:05.100 --> 00:36:13.260 William Cheng: That this to add eventually it's going to take his time executing the you know the code. Over here, over here says the CO execute over here is going to be mutually exclusive. 262 00:36:13.590 --> 00:36:16.380 William Cheng: Right, because no other thread will be execute in parallel, you know, in the 263 00:36:17.280 --> 00:36:25.920 William Cheng: MPs MPs over here. Okay. When it's done over here, what it would do is copy of the of so when a copy of s is going to increment is equal to x plus one by one. 264 00:36:26.250 --> 00:36:37.950 William Cheng: Okay, so as well become one as soon as they become one. One of those 99 threads going to see though it's see that equal to one and that will be the thread that will change it into a zero. So this way. No other thread will see the value of SP one 265 00:36:38.670 --> 00:36:43.410 William Cheng: Guys, but again as soon as this goes and other side is gonna come in. So that's why this function is known as one at a time. 266 00:36:45.330 --> 00:36:51.450 William Cheng: Alright, so again, this cow, you know, even though it's very simple, it's very tricky because it would be because it require you to understand 267 00:36:51.750 --> 00:37:00.210 William Cheng: The P operation and the operation right so this way. If you have many, many threads called one at a time. Simultaneously, only one that can be executing the code in the middle of here. 268 00:37:00.540 --> 00:37:06.960 William Cheng: Okay. So in a way, this is the same thing as the critical section called. Okay. So in this case, what would be the difference between using tags and using a center for 269 00:37:07.230 --> 00:37:21.060 William Cheng: Well, so in that case actually be exactly the same. I can run my co this way piece for me attacks lock x equal to x plus one piece of music on La, la, or I can do POS x equal x plus one vs. Okay, so these two co will be exactly the same. 270 00:37:22.260 --> 00:37:32.520 William Cheng: Okay, so. So this guy is the guy. This is known as the binary said before, because the Center for the value of the seven what valiquette and take it go from 1202 from zero to 1101001 271 00:37:33.210 --> 00:37:36.990 William Cheng: Okay, so that's called a binary, binary center for. And typically what we use the binary some of 272 00:37:37.380 --> 00:37:44.640 William Cheng: Us some of what we don't use it this way. Okay, if you want to use it this way, we might as well use them your tax okay because in this case it will be exactly the same. 273 00:37:45.480 --> 00:37:55.620 William Cheng: Right. Alright, so if you use it this way. There's really no difference between a summer for enemy attacks. Okay. So typically we see the US be in a different way. So this code is called n at a time over here. 274 00:37:55.890 --> 00:38:00.000 William Cheng: And December for is known as accounting some of all we're going to start with. And so let's say you go to three over here. 275 00:38:00.570 --> 00:38:06.540 William Cheng: Okay, n equal to three. I have 100 thread over here, calling enter the time simultaneously, what's going to happen, right. 276 00:38:06.810 --> 00:38:12.060 William Cheng: Once that is going to see an equal to three, it would recommend good does too. And then I thought, will be executing the middle over here. 277 00:38:12.480 --> 00:38:18.510 William Cheng: Another thread is going to see the same have a gala being go to two and he would recommend into one and it will start executing over here. 278 00:38:18.750 --> 00:38:25.560 William Cheng: Another thread will see the seven for valid being one it will change into a zero, and then it will execute over here. All the other 9397 279 00:38:26.070 --> 00:38:31.320 William Cheng: They all got stuck at POS because all the other 97th where they all see the Sun Valley equal to zero. 280 00:38:32.040 --> 00:38:36.750 William Cheng: Right. So again, this, this works this way because of the definition of PMS. 281 00:38:37.260 --> 00:38:50.040 William Cheng: Then or so in this case when any threat over here. When it's done. It's gonna call the of s is going to increment from us from zero to one. As soon as it does that another thread is going to come inside over here. So, therefore, in the middle code over here we have a parallel ism of three. 282 00:38:51.180 --> 00:38:57.120 William Cheng: Okay, so in this case if it has a parallel ism of three is this code equivalent to using the new tax. 283 00:38:57.510 --> 00:39:04.920 William Cheng: One know right because if you use piece of meat AX talking piece of music and log over here. Well, in that case, you can only have a problem with them a one. You can never have palace. Number three. 284 00:39:05.790 --> 00:39:16.860 William Cheng: Okay, so therefore there's no as a county some of what we're going to use county center for to solve the producer consumer problem now. So therefore, the main difference between seven for a few times over here. There. Alright, so 285 00:39:17.760 --> 00:39:25.620 William Cheng: So, so, you know, this code clearly is not equivalent to using a new tag. So the, so the orange one. One difference over here if you want to see a problem with him, have more than one 286 00:39:25.890 --> 00:39:35.640 William Cheng: One this case you will use the Center for you. We will not use the new tax now. All right. And also, one thing that happens over here is that, you know, the thing that caught piece of me tax law right so 287 00:39:36.390 --> 00:39:43.800 William Cheng: If we go back to the previous slide right there. Right. The third that copies of new tax law is going to be the thread that copies or matrix unlock 288 00:39:44.820 --> 00:39:52.920 William Cheng: Okay, but when you're using accounting summer for typically they're not going to be used. This way, what we're going to see is that the typical application for County summer for is that 289 00:39:53.100 --> 00:39:59.640 William Cheng: The thread called the P operation over here will not be the same thread that do the V operation. Okay, well actually be a different 290 00:40:00.420 --> 00:40:08.400 William Cheng: Or the other ways to think about is that the thread that does the P operation of the summer for typically what we'll do is that it will perform the operation on a different server for 291 00:40:09.420 --> 00:40:14.550 William Cheng: Or so. So under that condition that's what you will see a typical use for 74 yeah 292 00:40:16.230 --> 00:40:18.960 William Cheng: Alright so next time we're going to do is that we're going to take a look at the 293 00:40:19.380 --> 00:40:27.210 William Cheng: Producing consumers that before. So in this case, we're actually going to use two different time of war. One is called empty and the other ones can occupy, and this is the code for the consumer. This go for the 294 00:40:27.870 --> 00:40:33.270 William Cheng: scope for the producer and on the right hand side, the producer for the Copa the consumer. So in this again. 295 00:40:34.620 --> 00:40:39.420 William Cheng: Let me just remind you one thing on the conveyor belt. There's only one producers threat and there's only one consumer threat. 296 00:40:39.720 --> 00:40:43.110 William Cheng: Okay. The example that we mentioned before, over here, I said there's 100 297 00:40:43.350 --> 00:40:56.310 William Cheng: Calling no time. Again, that's not the producer consumer example for the producer consumer example, there's only one producer through and there's one consumer threat right the producer threads going to call corporate deals over here to synchronize with the consumer threat. 298 00:40:56.790 --> 00:41:05.250 William Cheng: OK, so the coda, we should over here the synchronization code over here, the producer usually is doing something else when it's ready to synchronize with a consumer, when does when does the wanting to synchronize 299 00:41:05.670 --> 00:41:09.120 William Cheng: With the consumer when they tried to put data onto the conveyor belt. 300 00:41:09.900 --> 00:41:17.580 William Cheng: Okay super. Lydia windows consumer over here, typically what it's doing is doing something else when it's ready to synchronize with a producer when it needs to come to the conveyor belt. 301 00:41:17.790 --> 00:41:21.690 William Cheng: pick out one item at that time is going to call this function over here to pick up the item. 302 00:41:22.380 --> 00:41:32.580 William Cheng: Okay, so at that time they need to synchronize so next time we're going to go over this code. We're going to sort of see how this will actually work and solve the producer consumer problem. Okay. Alright, see you in lecture five. Okay.