#20 Golang - Concurrency: Mastering Mutexes and Confinement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] Welcome to our episode on mastering concurrency in go with mutexes and confinement today we'll dive into the world of go routines mutexes and confinement and learn how to handle shared resources safely and efficiently imagine a concert with limited tickets and hundreds of eager fans trying to buy them simultaneously without proper management this can lead to overbooking or ticket duplication similarly in programming accessing shared resources without synchronization can lead to race conditions and unpredictable results let's look at our ticket booking system that we are going to code in go here we will Implement multiple go routines that represent users and try to purchase tickets we will use the shared variable tickets and access it by each go routine without synchronization let's say we have 500 tickets available and about 2,000 users are trying to buy the tickets we will use goroutines to simulate a lot of users trying to purchase the tickets at a time let's begin we need a weight group to block our code to allow a set of goroutines to complete execution in this Loop we will create G routines for each user let's increment the weight group counter by one using add method now we create a goutine Suppose there is a function by ticket this function will require the weight group user ID and the reference to the shared variable ticket we pass this by reference as we will have to decrease the ticket count when a ticket is sold now we will implement the function buy ticket this function takes three arguments the address to the weight group the ID of the user who is trying to purchase the ticket and the address to the remaining tickets count we first check if there is any ticket left if tickets are remaining we simulate the ticket purchase by decrementing the ticket count next let's add a print here and if there is no ticket left we add another print at the end of the goroutine we would like to Signal the weight group that we are done here let's do this at the beginning of the function with defer in the main function we will wait for all goroutines to finish with the weight function let's run the program whenever a ticket got sold we have put this print this print will always have this text in it we pipe it with a grip to get only these prints now let's pipe this to count how many tickets got sold what how did we sell 58 tickets we had only 500 tickets let's run this command 10 times we can see there is inconsistency in the numbers our shared resource tickets variable is not protected and is used simultaneously by many goroutines let's fix this using a mutex in the goroutine we have been using this shared variable without protection we will solve this with a mutex let's create a mutex here now in the G routine where the shared variable is used we will protect that part of The Code by locking the mutex let's run the program now this works properly it sells exactly 500 tickets another powerful technique to solve such problems is confinement it restricts access to the shared resource ensuring it's only accessible in a controlled area of your program let's see how we can restrict access to the tickets variable using confinement this is the tickets variable this channel is used to send ticket purchase requests and this one to Signal the stop this function will be used as a go routine this manages the allocation of tickets this function simulates user requests trying to buy the ticket let's start with the main function we start the go routine manage tickets this function needs the ticket Channel as it looks for a new user request on this channel we will create an infinite Loop here next look for a new user request in the ticket Channel we will need the tickets variable to manage tickets let's add it as a new argument if tickets are available allocate a ticket by reducing the count and add a print as we did before if tickets are not available add another print let's fix the function call here in the main function now we will Loop over all users suppose 2,000 users are trying to buy the ticket simultaneously increment the weight group count now create a new gutin here with the function buy ticket this function needs the ticket channel to send requests to the manage ticket goutine so add it as an argument let's send the request to the channel oh we will need the user ID as well now send the user ID to the channel we will signal the weit group once the go routine is over we need another argument weight group here now call the done function on the weight group let's fix the function call in the main function in the end let's wait for all goroutines to get over by calling the weight function on the weight group well there could be a scenario when fewer users are trying to buy the ticket in such a case we need to Signal the managed ticket go routine that we are done with the requests we will use the done channel to do so in the end we send true to the done Channel let's handle this in the manage ticket function in the infinite Loop we will have a select statement to handle both cases of receiving a ticket request and a done signal this this is our first case the second case is if done is triggered add a print with the remaining tickets here we need to fix the function call now run the program and looks like it is working let's run it 10 times and see how many tickets are being sold perfect it sells exactly 500 tickets now consider a scenario where fewer users are trying to buy the ticket Suppose there are 200 users only now run the program it sells 200 tickets exactly let's look at the output of the program and here it says 300 tickets are left perfect today we explored how to handle concurrency in go using mutexes and confinement we learned that proper synchronization is crucial for ensuring consistent access to Shared resources whether you prefer the direct control of mutexes or the structured approach of confinement go provides the tools you need to manage concurrency with with confidence thank you for watching happy coding
Info
Channel: codeHeim
Views: 2,845
Rating: undefined out of 5
Keywords:
Id: piutgCAvUjw
Channel Id: undefined
Length: 14min 34sec (874 seconds)
Published: Tue Feb 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.