WEBVTT 1 00:00:32.160 --> 00:00:37.980 William Cheng: Good morning this is LIVE lecture number five on June 4 2 00:00:39.480 --> 00:00:41.850 William Cheng: If you have questions, feel free to ask 3 00:01:40.560 --> 00:01:43.050 Lalit Gupta: So I have a question. Sure. 4 00:01:44.070 --> 00:01:47.520 Lalit Gupta: So in another slide you are talking about like you sleep. 5 00:01:48.600 --> 00:01:53.280 Lalit Gupta: If you sleep at night, not the entrance, or in a lake or what 6 00:01:55.320 --> 00:01:59.850 William Cheng: I think you sleep is pretty safe to use under all kinds of conditions. 7 00:02:02.130 --> 00:02:02.970 Lalit Gupta: So you 8 00:02:04.350 --> 00:02:05.640 William Cheng: Save it all kinds of stuff. 9 00:02:06.750 --> 00:02:07.320 William Cheng: Okay, so 10 00:02:07.680 --> 00:02:13.440 William Cheng: As far as Ubuntu 16.4 is concerned, we don't know if it's true in, you know, in all the other operating system. 11 00:02:14.760 --> 00:02:24.840 William Cheng: But for one to 16.4 which is the only platform that your code should work on, you know, for that it's it's it's retention is safe. It's everything 12 00:02:25.860 --> 00:02:33.120 Lalit Gupta: So you're saying there's some there can be some problem with usually like ours aren't able to understand what what can be the problem that usually 13 00:02:33.540 --> 00:02:50.220 William Cheng: What it just that on some operating system. You know, it's ambiguous. Right. Will you say I want to say for, you know, three seconds. What do you feel running multithreading you know maybe you are evenly divided three seconds. Mom, I mean it sounds weird, but there are some system like that. 14 00:02:51.480 --> 00:03:00.720 William Cheng: Okay, so, so it's just not universal. But, you know, for for our case, since we only have to run all been to 16.4 then you know 15 00:03:01.440 --> 00:03:06.990 William Cheng: I think they they they think either the man page or somewhere says that 16 00:03:07.290 --> 00:03:17.850 William Cheng: You know, if you no matter how many thirds, you're running. If you say I want to wake up three seconds later. Well, the system will wake you up, you know, the current time plus three seconds later, but again, no guaranteed exactly what you're going to wake up. 17 00:03:18.720 --> 00:03:21.000 William Cheng: But it doesn't really matter how many so I do run in parallel. 18 00:03:24.450 --> 00:03:29.520 Lalit Gupta: And also, like is it does this when we do man pages or something. 19 00:03:31.020 --> 00:03:37.050 Lalit Gupta: Does the mandate also specify whether the code is the entrance or not. Do we have to like look up somewhere else. 20 00:03:37.980 --> 00:03:41.730 William Cheng: Well, usually you assume that the code is retention. 21 00:03:43.650 --> 00:03:47.760 William Cheng: You know all the UNIX libraries, they make a thread safe, long time ago. Right. 22 00:03:49.260 --> 00:03:59.430 William Cheng: So again in warm up to you if you're asking about warm up to warm up to, it's a multi threading assignment. So under multithreading re entering a thread safe. It's the same thing. 23 00:04:01.290 --> 00:04:05.730 William Cheng: Okay, so the UNIX Library and the library, you know, they made it safe. 24 00:04:06.750 --> 00:04:12.060 William Cheng: You know, long time ago. But of course, you're not there are certain things. You're not allowed to do, right, for example, if your thread. 25 00:04:13.230 --> 00:04:13.830 William Cheng: You know, 26 00:04:15.510 --> 00:04:20.940 William Cheng: Like the example that I mentioned before, if you have to threats are joined with the same thread, well then you, you have a bug in your code. 27 00:04:23.460 --> 00:04:30.480 William Cheng: Right. Some people say, Oh, no, the code is not retention. The code is not thread safe but but it's maybe a simple as, you know, you have a bug in your code. 28 00:04:31.170 --> 00:04:41.460 William Cheng: For example, if you free, the same data structure toil you so if if you free, the same point or twice. Well, you know, again, it's a bug in your code. It's not that because free is not safe. 29 00:04:44.040 --> 00:04:45.390 William Cheng: Did you know what do you know what I mean. 30 00:04:48.930 --> 00:04:50.340 Lalit Gupta: You know, can you read one more time. 31 00:04:50.940 --> 00:04:57.060 William Cheng: Well, so for example if you call Mela you get a pointer. Right. And now you share this pointer with two threads. 32 00:04:57.840 --> 00:05:00.450 William Cheng: Oh yeah, both let's call free on it. 33 00:05:01.320 --> 00:05:08.640 William Cheng: And you'll program crash. Some people say, oh, they are. That's because three three is not safe. No, it's because there's a buggy. 34 00:05:09.840 --> 00:05:12.300 Lalit Gupta: Yeah, once you once one, one of the tech calls 35 00:05:12.330 --> 00:05:15.060 Lalit Gupta: Freedom other college should not call free 36 00:05:15.540 --> 00:05:25.590 William Cheng: Right. And also, you know, for the pizza library. Right. So. So for example, if you call piece of meat has unlocked when you don't have to meet x lock, then it's are safe, right, because you have a bug in your code. 37 00:05:27.600 --> 00:05:34.920 William Cheng: Yes. Okay, so. So again, just because things are thread safe doesn't mean that you can use it anywhere you want. You still have to use it correctly. 38 00:05:39.120 --> 00:05:39.720 Lalit Gupta: Okay, and 39 00:05:41.610 --> 00:05:50.160 Lalit Gupta: During the main page we can figure out what is how, what is the function actually do. And suppose I want to figure out like how what 40 00:05:51.330 --> 00:05:57.870 Lalit Gupta: For example, you're talking about like struck time. Well, is it possible to do man of that also 41 00:05:58.770 --> 00:06:08.670 William Cheng: On. No, no. So data structure don't have their man pages, the function that uses them has a man page. Okay, so what you will do is that you can you can do a man get time of day. 42 00:06:09.510 --> 00:06:13.260 William Cheng: Okay, so good time of day is going to sort of to tell you what the structure look like 43 00:06:13.920 --> 00:06:20.730 William Cheng: They're also function like you know time or something like that. See time, right, so remember see time also work with the same or no. Yes. 44 00:06:21.330 --> 00:06:25.050 William Cheng: No, no, sorry. See time. Start with the integer and you convert that into a time, nevermind. 45 00:06:25.830 --> 00:06:35.820 William Cheng: So I think if you look at the time of day and then it will talk about struck time Val. And then what you do is that you look at the main page and you scroll all the way to the bottom at the bottom, there's something called. See also 46 00:06:36.780 --> 00:06:41.820 William Cheng: Okay, so in the. See also function, they will mention other men pages that my talk about something similar. 47 00:06:43.650 --> 00:06:54.060 William Cheng: Okay, and they can do men on the other end on, on the other function. So maybe some men pages. That's more documentation on the data structure and some some doesn't 48 00:06:56.340 --> 00:07:05.370 William Cheng: Okay, you can you can also just ask me, right, because you know struck time val is a very simple data structure. He has two integers in them. One is a number of seconds. And the other was number of microseconds that that's all it is. 49 00:07:07.590 --> 00:07:12.330 William Cheng: So it just, it's you know, it's not even an abstract data type, he just, he just really pretty. 50 00:07:12.330 --> 00:07:13.140 Lalit Gupta: Simple. Yes. 51 00:07:13.980 --> 00:07:24.180 William Cheng: Yes. So you so you need to look at the main page of the function are going to use that because what they will do that they will manipulate the data inside the you know the data structure. Okay. 52 00:07:29.730 --> 00:07:31.110 William Cheng: Thank you. Okay. 53 00:07:40.350 --> 00:07:41.610 Wookyum Kim: Purpose arrival constraints. 54 00:07:44.190 --> 00:07:44.820 Wookyum Kim: In Slide 55 00:07:44.880 --> 00:07:48.000 William Cheng: 2727 okay 56 00:07:50.160 --> 00:07:53.400 Wookyum Kim: It's about the conditioner variables. So, 57 00:07:55.950 --> 00:07:57.240 Wookyum Kim: Yeah, I was. 58 00:07:57.330 --> 00:08:02.700 Wookyum Kim: kind of confused that condition variable is a queue. Right. 59 00:08:02.850 --> 00:08:03.330 Wookyum Kim: But 60 00:08:03.960 --> 00:08:11.040 Wookyum Kim: The queue have to be I think you have to be place the outside of the serial Asian box right 61 00:08:11.670 --> 00:08:22.560 William Cheng: So I, I joined. I joined like this intentionally right so this way when you're asleep outside asleep inside the queue, then it's clear that it's not inside the serialization box. 62 00:08:23.550 --> 00:08:24.000 But 63 00:08:26.400 --> 00:08:30.240 Wookyum Kim: Variable shouldn't be in their cereal box. I mean, 64 00:08:30.720 --> 00:08:38.250 William Cheng: Right. So this one to say that you can only touch the condition variable, just like you know when you when you try to touch a, b, and c, you have to have the new tax law. 65 00:08:41.910 --> 00:08:46.800 William Cheng: Okay, so, so if you're outside or you try to touch the CV. Well then, then, then you're going to cause trouble. 66 00:08:48.510 --> 00:08:48.990 Wookyum Kim: Okay. 67 00:08:52.500 --> 00:08:53.070 Wookyum Kim: So, 68 00:08:54.120 --> 00:09:05.940 Wookyum Kim: Okay, because we are using the the piece rat library. So we don't really care about the conditioner variable should be inside the text or not. Right. 69 00:09:06.870 --> 00:09:11.820 Wookyum Kim: We don't, we don't really need to care care about because that it's built in the library. 70 00:09:13.290 --> 00:09:20.430 William Cheng: Yes, but, but, you know, on the right hand side, this picture. I mean, you don't know, there's no serialization box civilization bug is just conceptual 71 00:09:20.670 --> 00:09:21.510 Wookyum Kim: Yeah yeah yeah yeah 72 00:09:21.660 --> 00:09:28.470 William Cheng: So you don't you don't really put anything inside of a that's just the way you should think about it is that if I if I, you know, if I have this tool. 73 00:09:28.710 --> 00:09:37.440 William Cheng: That show you this picture. It's a reminder that you shouldn't touch the CV, unless you are, you know, unless you are the owner of the new tax. 74 00:09:37.590 --> 00:09:41.280 Wookyum Kim: Oh, okay. So serialization boxes, not on mute tax. 75 00:09:42.420 --> 00:09:43.200 William Cheng: The civilization. 76 00:09:44.460 --> 00:09:46.290 William Cheng: The serialization, but he's just a notation. 77 00:09:46.980 --> 00:09:47.580 Wookyum Kim: Yeah. 78 00:09:47.640 --> 00:09:55.290 William Cheng: This is the new tax, right. So, so every new tax right every new tax has a new text queue and conceptually, you know, there's a civilization box. 79 00:09:55.650 --> 00:09:56.880 Wookyum Kim: Okay, okay. 80 00:09:57.090 --> 00:10:00.630 William Cheng: So you need to decide what variable to put inside the civilization box. 81 00:10:01.230 --> 00:10:02.580 William Cheng: And they follow the rules. 82 00:10:03.120 --> 00:10:03.960 Wookyum Kim: Yeah yeah 83 00:10:06.060 --> 00:10:09.570 William Cheng: So I guess that that's why I put civilization box in double quotes, so it's 84 00:10:11.370 --> 00:10:12.270 William Cheng: Just like quoting 85 00:10:13.500 --> 00:10:14.040 William Cheng: I see. 86 00:10:14.310 --> 00:10:18.600 Wookyum Kim: And then slide 31 okay yeah oh 87 00:10:23.340 --> 00:10:39.480 Wookyum Kim: Yeah so piece thread conditioner condition. Can't wait functions. The last time you text and wait for events should be must be at to me in here is, but I don't really understand that. Why have to be 88 00:10:41.040 --> 00:10:42.270 Wookyum Kim: At Tommy in here. 89 00:10:42.780 --> 00:10:51.120 William Cheng: Yeah, so that's a few slides down over here. Right. Right here. Right. It says that if piece that condition where it is not atomic 90 00:10:52.650 --> 00:10:59.160 William Cheng: Then it is possible. Somebody will signal Brock has the condition in between lock unlock in a new tab and wait for The Event. 91 00:11:00.600 --> 00:11:11.070 Wookyum Kim: I'm okay. But maybe later, there will be another Seguin there. But that's still a race conditions right what 92 00:11:11.130 --> 00:11:11.550 William Cheng: If this 93 00:11:12.750 --> 00:11:16.440 William Cheng: If this signal is a once in a lifetime signal. Well, they mess it 94 00:11:17.070 --> 00:11:17.520 Okay. 95 00:11:18.840 --> 00:11:29.850 William Cheng: So in order for a man, you know, in order for your code to be perfectly safe, then you know working every time you have to do it. Right, right. So in this case, the Peter library also have to implemented correctly. 96 00:11:30.510 --> 00:11:33.090 Wookyum Kim: Is that the only reason for it should be that to me. 97 00:11:36.090 --> 00:11:47.940 William Cheng: What this is just an example where things won't work. Right. So, so if there's one case that things won't work. Then, according to Murphy's Law, it only happens when something is very important. 98 00:11:48.630 --> 00:11:50.040 Wookyum Kim: Okay. Okay. Thank you. 99 00:11:52.440 --> 00:11:53.220 Wookyum Kim: Yeah, slight 100 00:11:53.310 --> 00:11:56.580 William Cheng: 5757 okay 101 00:12:03.000 --> 00:12:03.360 William Cheng: Okay. 102 00:12:03.840 --> 00:12:07.710 Wookyum Kim: Yeah, in here. Um, yeah, so 103 00:12:09.840 --> 00:12:14.430 Wookyum Kim: So it's wrong code right it it doesn't 104 00:12:14.520 --> 00:12:15.240 William Cheng: It doesn't work. Yeah. 105 00:12:15.270 --> 00:12:18.330 Wookyum Kim: Yeah, but I don't understand why it doesn't work. 106 00:12:19.560 --> 00:12:19.950 Wookyum Kim: Oh, 107 00:12:20.130 --> 00:12:26.670 William Cheng: Yeah so. So let's say that you have only one CPU. So I mean, when the code works, it should work with one CPU two CPUs. Many CPUs. Right. 108 00:12:27.060 --> 00:12:43.500 William Cheng: Yeah, so if you only have one CPU. So let's say the n equals two, five guys have four threads right want 123443 sleeping in the barrier. Q When the fifth rest comes in. Over here, it will execute as Co Peter condition broadcasts. 109 00:12:44.280 --> 00:12:50.910 William Cheng: Okay, so they will reset the counter zero and now the fifth threat number number five is going to leave the barrier synchronization. 110 00:12:53.970 --> 00:12:59.550 William Cheng: And then all these four says when they wake up, they will see count equals zero. So they all go back to sleep. 111 00:13:02.310 --> 00:13:02.820 Wookyum Kim: What 112 00:13:06.660 --> 00:13:11.580 William Cheng: Right, so. So again, if you have, if you have only one CPU. You can only will execute one third of the time, right. 113 00:13:11.880 --> 00:13:12.330 Wookyum Kim: So when you 114 00:13:12.360 --> 00:13:20.940 William Cheng: Execute the thread. Number five. Number five is going to, you know, wake up all these four threads. But when you wake them up. They're still sleeping right because the CPU can only do one thing at a time. 115 00:13:27.570 --> 00:13:32.820 Wookyum Kim: If, then, what if, what happens if that there is a vertical CPU, then maybe words. 116 00:13:32.910 --> 00:13:42.120 William Cheng: So in this case, again, it works in multiple CPUs are also working on a single CPU. The reason that it works in multiple CPU is that thread number five over here has a metric locked 117 00:13:43.020 --> 00:13:47.880 William Cheng: So therefore, even if you if you work out the other four threads. The other four threads. They don't have the new tax. 118 00:13:48.330 --> 00:13:48.660 Wookyum Kim: Yeah. 119 00:13:48.930 --> 00:13:57.180 William Cheng: So therefore, they will not execute right okay when certain number five copies me test unlock. Then another thread will start executing critical section code. 120 00:13:59.070 --> 00:13:59.430 William Cheng: Okay. 121 00:14:01.500 --> 00:14:18.780 Wookyum Kim: So that's that is going to be either 3123 or four and that's that will return from piece of a condition. Wait, rather, this will you return from Peter of initial way, you have the new tax law, but it. I mean, but it or Luddy are but broadcast is that waking up that that 122 00:14:19.500 --> 00:14:22.590 Wookyum Kim: Cue up the new tax not cure the barrier here. 123 00:14:22.860 --> 00:14:23.790 William Cheng: Yeah, you move them up. 124 00:14:23.880 --> 00:14:27.690 William Cheng: And you want to. So there's still that all these four threads waiting for the new tax. 125 00:14:31.140 --> 00:14:35.430 William Cheng: The when one of the threads 1234 get the new tax it will return from Peter condition. Wait. 126 00:14:37.650 --> 00:14:39.060 Wookyum Kim: Oh no wait, um, 127 00:14:39.240 --> 00:14:42.600 Wookyum Kim: But in here barrier accuse the condition be reversed. Right. 128 00:14:44.010 --> 00:14:46.860 William Cheng: Yeah, the bear a few over here is a condition variable. Yeah. 129 00:14:47.160 --> 00:14:48.510 Wookyum Kim: Yeah, then why don't 130 00:14:48.540 --> 00:14:53.550 Wookyum Kim: Condition broadcast cannot wake up the error, who in here. 131 00:14:54.000 --> 00:14:57.510 William Cheng: Yeah, there are woken up but they're sitting in the new tech skew. 132 00:14:59.280 --> 00:15:06.690 Wookyum Kim: Okay, but then why you why. And then counter counter becomes zero and then 133 00:15:08.880 --> 00:15:09.360 Wookyum Kim: Oh, 134 00:15:10.200 --> 00:15:12.360 Wookyum Kim: Sorry, I will. I'm confused now. 135 00:15:12.570 --> 00:15:16.440 William Cheng: Right, so, so let's take a look at this picture, and here's a serialization box. Right. Okay. 136 00:15:16.770 --> 00:15:24.180 William Cheng: Right here is the condition variable. Q. We have 31234 sleeping over here. Let's cue over here is empty. Right. 137 00:15:24.480 --> 00:15:27.720 William Cheng: Yeah, so number five is executing inside of civilization box. 138 00:15:27.870 --> 00:15:46.830 William Cheng: Okay okay so so square number five says piece or a condition broadcast. So all these 1234 are gone. They all they all come here right 1234 and I'm still here. Okay. So yeah, that's right. Number five is going to execute piece for me is unlocked. So thread. Number five is gonna is gonna leave 139 00:15:47.190 --> 00:15:52.380 William Cheng: So in this example, it will bring in three number one. So step number one is going to be execute here. Okay. 140 00:15:52.770 --> 00:15:56.520 William Cheng: Okay, so what kosher to execute with their number one come into the sterilization box. 141 00:15:57.870 --> 00:15:58.950 Wookyum Kim: Um, 142 00:16:00.000 --> 00:16:05.460 Wookyum Kim: If count. I mean, our peace read new tax law, and then he counts. 143 00:16:05.790 --> 00:16:10.290 William Cheng: Was also no no it doesn't return on piece of meat as he returned from Peter condition way right 144 00:16:12.390 --> 00:16:13.290 William Cheng: Because last 145 00:16:13.530 --> 00:16:20.460 William Cheng: Three number one did was copy through a condition way they unlock the new tabs and go to sleep in a condition variable killing one atomic operation. 146 00:16:20.940 --> 00:16:24.570 William Cheng: Okay, though, so it's a function called right so they will return from the function call. 147 00:16:25.020 --> 00:16:26.100 William Cheng: So when that one. 148 00:16:26.400 --> 00:16:29.700 William Cheng: start executing again, you know, it will return from Peter condition. Wait. 149 00:16:30.390 --> 00:16:43.080 William Cheng: Oh, and then he will check counties. What a zero and is equal to five. So therefore, they will go back to sleep again. Right, so one over here is going to go back here and threat to is going to come in. So I to also go to sleep and thread three so they all do the same thing. 150 00:16:44.190 --> 00:16:45.240 Wookyum Kim: Oh, 151 00:16:45.270 --> 00:16:47.490 Wookyum Kim: Because it's while I see 152 00:16:48.270 --> 00:16:50.910 William Cheng: One of them can be executed in critical section co at a time. 153 00:16:51.420 --> 00:16:52.050 Oh, 154 00:16:53.130 --> 00:17:07.800 Wookyum Kim: I see. So in that previous one. That was our so that the wrong was but that can be like work because it's not a while states ones in here if that's if conditions. So, 155 00:17:09.180 --> 00:17:17.640 Wookyum Kim: Yeah, so, so, so this function can return for no reason. And now this thread, don't leave the barrier synchronization while it was never woken up 156 00:17:18.060 --> 00:17:23.820 William Cheng: Right. Nobody called Peter condition signal a piece of content broadcast. So in that case, the co executing correctly. 157 00:17:26.340 --> 00:17:27.720 Wookyum Kim: Okay. That's because 158 00:17:29.100 --> 00:17:35.280 Wookyum Kim: That's just like the piece right libraries, not like built in this way. Right. 159 00:17:36.090 --> 00:17:36.420 William Cheng: Well, 160 00:17:37.410 --> 00:17:48.210 William Cheng: So again, the piece of the library implemented to say that, well, you know, the correct way to use piece or a condition way is to do while Nagar piece recognition way. Okay, so this is a programming bug right 161 00:17:48.870 --> 00:17:49.320 Okay. 162 00:17:56.490 --> 00:17:56.820 William Cheng: Thank you. 163 00:17:58.320 --> 00:17:58.560 Wookyum Kim: Okay. 164 00:18:14.310 --> 00:18:20.850 William Cheng: You can also ask question about, you know, warm up to, you know, lecture slides. Anything you want to hit anything you want to ask 165 00:18:49.350 --> 00:18:50.880 William Cheng: No, nobody else has questions. 166 00:19:19.890 --> 00:19:30.030 William Cheng: Okay, if nobody else has questions is, okay, that we close the lecture or you're still scanning some slides or something like that, trying to find questions. 167 00:19:42.840 --> 00:19:52.680 William Cheng: Okay, so in that case, I'm going to close the lecture. So, I still have officer at one to 2PM so so feel free to come to office hour today. Okay. Oh. 168 00:19:52.890 --> 00:19:53.490 Professor 169 00:19:54.840 --> 00:20:02.610 Wookyum Kim: Because I'm still like some troubles with that finding a team members so 170 00:20:03.450 --> 00:20:04.770 Um, 171 00:20:05.820 --> 00:20:23.490 Wookyum Kim: Because we're not really like go into the classrooms to get along with the other students or so. So like, um, maybe can we like like talk to each other after the class like in the zoom meetings. 172 00:20:25.980 --> 00:20:27.570 William Cheng: Even in the you and I talked about it. 173 00:20:28.050 --> 00:20:36.540 Wookyum Kim: I mean i mean i mean i mean i mean we the other students like just have the students can you like talk about the team. 174 00:20:37.710 --> 00:20:38.580 William Cheng: Okay. 175 00:20:40.230 --> 00:20:44.850 William Cheng: All right, so, so you want me to watch this meeting is recorded. There's no you know so 176 00:20:44.910 --> 00:20:45.690 Wookyum Kim: Oh really, 177 00:20:46.590 --> 00:20:49.440 William Cheng: Okay. One thing that I can do that. I can turn off recording 178 00:20:50.010 --> 00:20:56.610 William Cheng: Yeah so i don't i think that that won't kill the meeting. So, so you can talk to other students. Okay. 179 00:20:56.940 --> 00:21:03.510 William Cheng: Okay. Alright, so I'm going to stop the recording and then I'm going to leave and then anybody want to visit me in the office are please go ahead 180 00:21:03.660 --> 00:21:04.830 Wookyum Kim: Yeah, thank you. 181 00:21:05.130 --> 00:21:06.180 William Cheng: Okay, bye.