Practical example using semaphores (Login Queue)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i want to take a look at a practical example as to where you could actually use uh semaphores uh namely we're going to try to implement a login queue you know when when a game is very popular and it's a multiplayer game you might have to wait at a in a queue to be able to log in simply because there are just too many people in that uh in that server that you're trying to log into so therefore instead of making the life miserable for everybody playing because the server is just not handling that many connections it's just gonna be a logging queue it's gonna make everybody wait in there for the limited amount of slots to be uh for some of them to be free so how do we do that well let's think about a situation where we have a simple system right and our system can only for some reason can only handle like let's say 12 12 people logged in at the same time so we have if we have 12 people logged in at the same time that's the maximum we can have we cannot do any more than that and let's say we have more than 12 trying to log in because otherwise it's not going to really make a difference i'm going to have here let's say 16 threads running at the same time trying to connect to this server quote unquote that we have okay i'm gonna print out some messages to figure out what's happening so i'm gonna use the the old code that we had here with the hello from thread and i'm gonna say before waiting right i want to say to the user that okay you're waiting in a queue so i'm going to say printf let's say waiting in the login queue backslash n i'm going to pass in here into pointer arcs because i want to also show the threat id otherwise it's going to be kind of difficult to figure out um okay so it is before the way that's something that all the trades are going to call we're going to see this message on all the trends now once we actually get past the same weight it means that we have decremented the semaphores value and we were able to actually login okay so i'm going to see here i'm going to say here let me actually just copy this line and say instead uh logged in logged in perfect that's fine and let's say the user stays online for a random amount of time so i'm going to have here a rand and say let's say percent five plus one so it's gonna take anything from one to five seconds for the user to actually log out once he is logged in okay that let's say we can control that and that's fine and once he finishes playing on that server that we have there then we're going to print a logout message let me see here logged out instead and we're going to remove this this doesn't really matter anymore then we're going to of course increment the semaphores value again using same post uh letting somebody else actually coming through and joining the server okay nice and because we have 12 value at the same time 12 users should be able to log in but then the other ones are going to have to wait for these users to finish uh playing to actually log out and if you try to launch this you are going to notice that in fact yes we should get here a lot of messages on the screen but at some point every single thread would have logged in and logged out so let's take a look here up top we have if we count them this should be 12 messages of logged in at the same time this is exactly the same moment more or less um that's exactly the amount of users that are allowed in the actual uh server at the same time and then the next ones you're gonna see that they just print waiting in the login queue and they're just gonna stay there until another thread logs out so we're gonna see that the next message has to be a logout message which it is see thread number three actually logged out that uh incremented the val the semaphore's value and was able to let another user log back in and there was a tread 12 which was actually waiting in here as we can see and sort of this back and forth for a couple times so you can see here thread number six logged out and then 13 was able to log in then thread number one logged out then 14 was able to log in and lastly uh 12 or thread number 12 logged out and then thread number 15 was able to finally log in our last user that we have and then afterwards we just have logged out messages because there's nobody else in the queue anymore and then the semaphore just some of us there just gets incremented back to 12 once all the users are out of the server and after the the program's execution finishes really we consider we don't have any user online anymore and this is more or less how games are handling uh logging queues they just have a semaphore on it it might be an intel processima for it might be a bit of a implementation where they have it on multiple servers it could you could do a lot of things we've done but at the at the core of it it's still gonna be a semaphore a symbol counter that just comes up and down uh back to zero and if it's zero it's gonna just make everybody wait we can change this for example this value from 12 we can change it to four and you're going to see again that if we started again only four were able to log in at the same time and then the rest are just waiting for in the login queue and as you can see the uh the program takes a bit longer to execute simply because we only have four slots and we're trying to manage 16 users right if you if we even had only uh two slots it would be even more slower so there you go i have two logged in and then as you can see this is incredibly slow actually it's going to take like uh something something around maybe 12 seconds i think to execute the whole thing or maybe even more anyway it it does take a long time because we only have two slots of course and with more slots if we have for example 32 slots all of a sudden you'll notice that all the threads are able to join the server and they just straight up log out after a while and that's it so really semaphores are used for uh limiting access to a resource so in our case what was the resource really the resource was our server quote unquote server we don't have an actual server but suppose that when you actually log in you actually do log into a server right um and that was a limited it has a limited computational power right you have like a couple cores you have a few gigabytes of ram but that's all you have you don't have any more so you have a limited number of users that you can let online okay that's that was our limited resource that we had and uh the semaphore was limiting users to connect or to use this resource um at the same time another way you can think about it is suppose you have a computer that has a four course right if it has four cores it has like 20 tasks to do of course those tasks are gonna have to be in a queue and they're gonna probably have to be uh synchronized using some sort of semaphore with the value four because only four um only four tasks could be running at the same time okay that's the rough estimate of course at the level of the cpu it's a bit more different than just uh using a semaphore from uh the api here but at its core the basis of it is the same that you have a limited resource the number of cores on a cpu and you have multiple things that try to access that that is the tasks in that case and you have to limit that and to do so we're using a semaphore i hope this was useful if you do have any questions leave them down comments below or on our discord server again the source code you're going to be able to find it on our website link in the description below take care bye
Info
Channel: CodeVault
Views: 3,239
Rating: 4.9722223 out of 5
Keywords: codevault, c (programming language), semaphore, example, practical, sem_init, threads
Id: Q79uEdKNVGY
Channel Id: undefined
Length: 9min 7sec (547 seconds)
Published: Tue Dec 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.