Semaphore Explained C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everyone today is going to be a short tutorial on the sem4 primarily i want to explain this concept before doing a tutorial on channels so this is going to be a prerequisite so without wasting too much time let's just jump into it and get one thing straight that the semaphore is for solving the synchronization between threads problem okay so it's not as scary as it sounds like semaphore stands for those guys with flags that go right but and if you google it you will see it but essentially i don't like thinking of it like that it doesn't build a really fulfilling image for me it's more of a like a gate for me to the city right where the city is the process okay so uh let's go ahead and do like a quick simulation okay so we're gonna start here and we are going to do some work okay and then we're gonna finish okay so we run this uh all is good start do some work nothing too complicated here what we now want to do is we want to put a gate on the process so let's say you're not allowed to enter here right and we're gonna start with zero we're basically gonna say right this is a city and no one is allowed to go there okay so that's what the gate is gonna be like so here we're gonna introduce sem4 slim there's semaphore and some four slim 74 is there for backwards compatibility so don't use semaphore use some for slim okay it's the newer implementation and it's better lightweight or faster or whatever right and we're gonna call it gate because that's the way i think about it okay and uh in the constructor you have to supply a number right and zero is the amount that are allowed to go through the gate okay so uh when we start uh what do we how do we utilize the gate right so what we do is we grab the gate and we say wait async right so we're going to wait till we're allowed to enter and for this you can see that this is asynchronous so it's basically a non-blocking weight so the thread is going to go off and do something else while it's waiting to enter okay so at this point if we run the application we're only allowed to start but we're not allowed to do any work okay so let's say we are allowing one person to go in right so he goes in and does some work nice so uh let's put this in a loop and kind of get an idea of how we can allow the next person to enter after one person has already entered right so we're gonna have ten let's go ahead put the loop here okay so uh we're gonna run this again and you will see that we get a start do some work finish and the second person that's trying to enter uh the limit here has an incremented backup after finish so this is what we want to do as soon as we weight async this number will drop to zero so this is the limit one person is in the process we now have to tell the people who control the gate essentially the gate itself that somebody has left the the building basically right they came they did some work and then they finished they left the building right and here we're saying only one person is allowed in the building what we could do is we could just go ahead and straight up increase this to 10 and everybody would be doing all the work all these 10 people that we're spawning but essentially what we want to do is we want to say right no only one person is allowed to do the work in the building because it's a very very small building right and we need to notify the next person when he's allowed to come in right and because this is an asynchronous operation the threat is gonna go off to do some other work right is like somebody's gonna turn up to the building he's gonna like can i do some work no okay i'm gonna go have a cup of coffee or something so uh the way we actually do it without rambling on for too long we just go ahead and call release okay so we decrement the number that we do here with weighty sync by allowing somebody to go and do some work and then we increment it back up by calling release okay so this kind of gives you a if i put this back to 10 right everybody does the work but as you've seen here we're only allowing one person in the building okay to do that work and this is not a very compelling real world example and i will give you a real world example right now okay where you do want this threat synchronization and the way i'm going to do it is i'm going to create an http client and this is a real world problem i had to solve so you have an http client let's go ahead and create the client we're also going to grab this gate as well all right we're not going to use it yet but we're going to have it here and this is going to be a new http client and what we're going to do is we're going to have a public async task what are we going to do we're gonna call google okay let's do this and we are going to have a response so we're gonna grab the client and we're gonna get async calling google here nice we're gonna wait here okay and then for the response we want to go ahead and grab the status code and just dump it okay one thing so basically i'm calling google here i know i haven't explained the problem but essentially what i'm going to show you here is we're going to make a ton of requests to the client and we're going to see some surprising behavior from the timeout okay so we're going to blast the client and the requests are going to time out before they're actually made right so it when we call get async that's not what the when the request is actually made by the network card that's when we call the function and the the timeout starts from the moment we call the function and your network card or the amount of ports that are open to a process is gonna limit how fast you can grab those requests okay so we're gonna basically blast the client and we're gonna see requests timing out before they can even be made okay so uh how do we do this so let's go ahead and create an innumerable where we're gonna yield some tasks and let's go ahead and say create calls and here for let's say like 2 000 i think 2000 would be enough for me or actually i don't want to spam google let's opt in for 200 and we're just going to put a really really small timeout and we're going to see how we can use the semaphore to regulate the threads that are going into all this client okay so let's go ahead and yield return calls google here and not calls but rather call google this is a semicolon and then in here we just want to task wait all and create calls okay and i guess we can go ahead and to array this so one thing i want to do here is let's grab the timeout and i will say new uh or rather time span and from seconds i'm just gonna put five right so we have five seconds for the request to complete and let's say i will put this to 10 first it doesn't matter for the content that we get back it just matters that these requests gets completed right so you can see all of the status codes are okay let's go bump this up back to 200 and let's see what happens and actually complete so my network card is fast enough let's go ahead for 500 okay and here is where you will see an exception of a task was cancelled so a little bit of a friendlier way that we can tackle this is let's say let's just put a try catch around this whole thing here say i want to catch the exception in and i will just grab the message and dump it right so we're just gonna know at some point we are gonna if i remember how to type uh at some point we're just gonna start hitting exceptions because stuff gets cut timed out so the error is not specifically too helpful either we're just gonna the operation we're gonna start seeing the operation was canceled so essentially what we want to do is we want to complete all 500 requests and not get the operation was cancelled so what and to restate the problem what you're seeing here is we create 500 tasks right and we basically kick off all 500 tasks and the computer is so fast to call this function because one thread comes in and then basically all right a network card go fetch this and then 500 that happens 500 times the network card and do it that fast so this function is called 500 times but the network card is only processing maybe like 10 or 20 requests okay so the timeout starts from the time you actually call the function not from when the network card starts working so that's when the task canceled operation is triggering from so what we want to do here is this is the process that we want to guard okay so here when we have the gate we're guarding some work right so we're protecting somebody from entering the building to do some work and then we're releasing so this is exactly what we want to do here and we want to essentially throttle how many requests can be made so we want to grab the gate and we're basically going to say right wait for [Music] um some free space so we don't blast the client so not all of our requests start timing out okay and then we're gonna grab the gate again and we're gonna say right once we actually produce the result or we've actually finished the request so i can actually put it here uh let's go ahead and just release okay and here this is again this is a bet this depends on your computer you can fine tune this and usually if you use it do this on local development if you then put in the client on the cloud or something like that it might have more network boards but again this is more of a specific issue with the http client and how it works uh at this point calibrating the sem4 is a trial and error kind of thing so you have to test it you have to figure out what's good what's not uh but logs etc so let's go ahead start with something like 20 right so can we handle 20 calls to google right let's go ahead and see this okay okay okay okay so you can see all the calls have have gone okay so what you could do is you could go overboard and you could say right we are going to allow 100 calls to go through the gate and maybe at some point you are still going to see some operations cancelled so this is what you want to test for when you're calibrating this number you can see most of them are good but some of them are still timing out and canceling so this is when you want to put the gate down up and usually again this will just depend on the machine that you're working on but yeah this is essentially the semaphore and what it does is protects a certain process asynchronously okay and as i explained it's like a gate and that's the way i like to think about it not the person with the flags giving signals it's more of just a gate to the city right if you enjoyed this tutorial leave a like subscribe don't forget to leave a comment if you have any questions also join the discord server i also do live streams on sundays and wednesdays so don't forget to join in for that links are in the description and hopefully i'll see you in my other videos
Info
Channel: Raw Coding
Views: 27,750
Rating: undefined out of 5
Keywords: c#, asp.net core, tutorial, c# tutorial, asp.net core tutorial, async, await, asynchronous, explained, how to, use, how, works, semaphore
Id: GKjM4AX8NME
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Wed Apr 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.