iOS Dev 17: Semaphore vs DispatchGroup | Swift 5, XCode 11

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys my name is emmanuel welcome to another video today we're going to learn about semaphores now in a couple of videos ago we learned about concurrency and how to execute multiple tasks on the background and then wait for all of them to be completed before performing one final action using the dispatch group right now in this video we're going to see an example of of when the dispatch queue isn't really the best solution for us right so the first thing we're going to do is to create two functions and i'm going to call the first one download movie all right and this is going to take a name like this of type string now i'm just going to return this name and uh what we're going to do here is sleep for four seconds and let's just print name has been downloaded awesome and finally we're going to return name wonderful now the second method we're going to implement is to save our movies or save movies well save movies and we're gonna sleep for let's say it takes two seconds and finally print movies have been saved cool all right now let's go ahead and practice what we learned about um dispatch groups so first of all we need to create a group so group is going to be equal to dispatch group like that and let's create a queue that's q be equal to dispatch um q dot so let's just use a global cue like that and next let's um initiate a download movie so we're going to say cue a sync now we're going to be using the group so group and pass in the group like this right and then i'm gonna have a closure and say self dot download movie i'm gonna be downloading the avengers avengers all right so we could actually assign this here to uh movie name be equal to this and well for now we don't need it but i'm just gonna add um an extra print statement so this is the q block and then movie name right for now this line isn't really very essential but no worries so the next thing i'm going to do is to add a save movies task to the queue so a sync well i don't know if i have time for this i'm just going to copy paste and we're going to call the method self dot save movies just like that so how do we get notified i hope you remember from from the other video you simply need to do group dot notify so tell me when and what do you want to tell me on the main thread of course and what do you want to do print all downloads have been completed beautiful so here i'm just going to save this system i ain't waiting for no man for no computer awesome all right now let's run this and see if things work properly okay so i ain't waiting for no man it's cool movies have been saved so it came over here and executed the save tasks first so movies have been saved avengers have been downloaded then they went back to the block all downloads have been completed so this worked as it should sorry hope you can see all of these now let's say um that we have a variable here that keeps track of the movies so i'm going to call this movies it's going to be of type string array and this is going to be equal to an empty array all right and now whenever our movie is completed what we simply need to do is say um movies dots append we're going to be appending the movie name just like that say self dot like this all right now right here whenever our movies are saved what i want to do is well not here actually in our um well i think we can do it here sorry so um right here whenever we call this let's say this method actually performs this saving we want to remove the first index so we're only saving um movies from the first index so after saving if i wanted to remove it from the array what i would do is say um self dot movies dot remove at index or remove at and then pass in zero right so by doing this i'm simply saying um the movie have been saved so remove the first index so just keep in mind that we are um only saving movies at the first index so at this point the download happens it appends it to movies at this point we are done saving and we're removing it from the movies array alright now let's run this and see what happens we are enjoying the video so far just take a second click on the subscribe button and uh let's continue very interesting it actually crashed now see what happened if you recall from here this guy takes just two seconds to execute it's going to be completed before this guy actually comes back right and these guys are actually sharing resources so even though i know this is a very simple example imagine that you have multiple threads all of them modifying the content of this movie array you can imagine that something will probably go wrong it's possible that um the the um array is updated before you actually before you were actually able to save or um in our case the movie hasn't even been appended and then you're trying to access the value at index zero so different complications can occur now in this scenario we want this resource to be accessed by um a thread one at a time now this is where semaphores come in what semaphores do is they allow us to be able to restrict or block a particular resource and only allow n number of threads to access that resource at any point in time all right now let's see how this can be implemented let's close this so um i'm just going to go ahead and comment all of these guys out and let's implement this over here all right so what we're going to do is first of all we need to create a semaphore and this is going to be equal to dispatch semaphore like that and it takes in an argument value and for now i'm just going to pass in one and explain what this is later on so then again we need to create a queue this is going to be equal to dispatch q dot global all right now cue the sync basically need to do pretty much the same thing as we did here we're gonna say let movie name oh well could just append it directly by saying self.movies.append and we're going to be appending self.download movie passing the avengers right awesome oh yep i need value here good and we're gonna do the same thing for our um save so let's get this guy let's comment this get it get out of the way and all right good so just like before we have um this task here to download the movie and then append it to the movies array and here we're simply going to save the movie alright but this time around we want this guy to wait for this one to be completed before it tries to save right now if we look over here in our stem before we have a value now this value is the number of threads that can access a resource at the time so since we have one here the first one thread to access the resource is going to be allowed if we had 10 it's going to allow the first 10. right we're going to see how this works again very so how do we wait very simple all we need to do is say semaphore i know how to spell this again right semi4 dot wait simple as that so this is going to wait for something for the resource to be available and what happens here actually is whenever this weight method is called it decrements the value of this by one and if the value is greater than or equal to zero it allows the um thread to be executed does that make sense okay so first weight allows the thread sorry weight decrements the value of the semaphore and if it is greater than or equal to zero it allows the resource to be accessed right now when we're done accessing the resource what we need to do is call our semaphore dot signal so this simply tells our semaphore that we're done with uh the resource and this increments the value by one so when the value is incremented by a one it checks if the previous value was um less than zero if it was it allows the weight for the next one to go so here we're gonna have semaphore the weight as well so um when we're done we're simply gonna say semaphore dot signal so let me go through the flow one more time at this point it decrements the value here and it's zero since it is greater than or equal to zero it allows this guy to execute all right when this is done this sends a signal that increments the value from zero so at this point it is zero it allows it pass and here it increments it back to one and when it does this this guy now decrements it to zero it's still allowed so the last is going to execute and then increments it to one now if we were to have um before we do that let's actually run this and see what happens okay so i'm waiting for no computer it's a big boy so you can see that everything actually works smoothly so i am waiting for no computer right here executed first where is that man all right this guy then after that he went ahead to download the avengers movie and then avengers has been downloaded all right cool after that this guy didn't execute because he was waiting and the moment this signal was sent this young man fired now let's see something else if i were to comment this guy out and not send a signal let's see if it actually goes ahead to save run it again so the download has been completed um two seconds should have elapsed by now so you can see that it actually didn't come here the reason is it's still waiting for this guy to make the resource available right and this is because we specified that the number of threads that can access this uh resource is one if we were to set this to like two and we run this again can you guess what happens it's gonna crash why because we specified that two of these guys can access that resource at the same time so this guy was done it was triggered and he just executed and then ran this guy so if we're to go step by step at this point it is two so here it decremented it to one one is greater than or equal to zero so it allowed this guy go right and while this guy was executing he came over here and decremented the value from one so the current value is one right decrement to zero and since zero is greater than or equal to zero it allowed this guy go right now in case these two don't believe me maybe emmanuel lies a lot let's do something else let's add a print statement here so i am in the first block before weight and um let's do this let's do this here so i'm in the first block after well i don't need to do it here sorry let's do it after wait after because after this we simply have a print statement so we don't need to do that anymore so we're going to do the same thing here but this time second block before wait and second block afterwards so what i simply want to do is to see so that you can see that it's actually waiting so i'm gonna call um set the value to one and let's see what we have now so you can see that first of all it executed this guy right so i am in the first block before wait and you can see that it also went to this guy and said i'm in the second block before weight but it didn't get past this weight because the value was less than zero so i'm the second block before weight i am in the first block after wait so this guy passed because remember the value at this point is going to be zero right so it passed and it executed this guy went ahead to download while this guy was still waiting and when it was done downloading it printed avengers has been downloaded all right pretty cool and since that has happened the signal has informed semaphores that a resource has been uh released or or free right so this now checked the previous value was less than zero so let's allow this guy go so the thread that's currently waiting let's allow that go so next i am in the second block after weight so this guy now executed and then the system just continued i hope i didn't confuse you with this extra uh breakdown but um if you didn't understand go ahead feel free to leave a comment in the section i'll definitely respond if you enjoyed this video go ahead and click on the like button if you haven't already subscribe turn on your notification because there are interesting topics i have in mind see you guys in the next video
Info
Channel: Emmanuel Okwara
Views: 1,337
Rating: undefined out of 5
Keywords:
Id: WNrBtBs9snQ
Channel Id: undefined
Length: 17min 6sec (1026 seconds)
Published: Sat Aug 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.