Okay so now we have a scanner that scans and reports.
But the problem with this scanner is that this country is very slow and we know that God is a very important
feature called concurrency.
So what we are going to do in this lecture is to use concurrency to build a more faster scanner.
So what we are going to do is we are essentially going to use out all of this functionality in a go
to Dean and then we will call this go routine.
But before we do that we have to make sure that the all the other things are done properly before the
main function is finished execution because if the main function finishes before these go routines are
run properly what will happen is that we won't see any results here.
And the good old things will run in the background and return the results.
But the main function will uh will have already finished executing.
So you won't see anything so let's create a function.
Guard function and this function will be let's take our scan board or something like that and this will
take the port number which would be bored and it would be of type Integer and then this function is
going to perform this functionality.
So I'm going to copy this functionality here.
I'm going to cut it and then I'm going to put it here so we can hard code the IP address here.
So we don't have to give IP address every time but you can modify the function.
And the next thing we need to do is instead of I we need to define port it should be port here and then
at the end we should close the connection probably so much and not close so the connection is not left
hanging.
Now we have put all our functionality that we defined here into this function and then we can simply
say go scan scan board groups scan board and then we can simply give the port number here and it will
run the program in concurrent.
But there is a problem with this program because if we run it like this what it will do is that it will
go to 100 iterations and it will exit and before we can check that if the if there is any port open
our main pull down will have finished.
So what we need to do is we need to introduce a weird function.
So Gore has a very efficient uh feature called Red Group.
So we know what we can do is that we can add create groups to the queue and then wait for them to finish
once every thing has finished.
It's like a counter so you add something to a counter and then wait for it to finish.
Once it has finished and once the counter comes back to zero and the main execution stops.
So what we can do is we can define the variable here
I'm going to call this variable as WG.
It represents where to grow up and then go has an internal package called Sing dot weight group.
So the type of the variable is seeing that weird look.
So it contains a weight group that will help us in keeping track of how many core routines we have started.
The next thing we need to do is we need to add elements to this weight group.
So weird group dot ad from Shannon will add one each time.
This loop is I treated so 1 hour ago voting has started it will going to this it will start adding and
counted and then once the goal voting has finished really accept this counter.
But if you go to this function you will you have to see that the scan board.
So once this god Odin has finished properly what we want to do is we want to finish on reduce this counter
here in order to do this we have to pass this object into this function as well.
So what I'm going to do is I'm going to pass a pointer to this weird group so it should be something
like this seeing the trade group.
So now this function has a pointer and then it can use this weird variable inside this function.
So funds it has finished completing is we can do it.
We can add a DFS statement had WG done done so what this function is when this function goes out of
scope it will reduce this column again.
So here we are adding this counter.
WG dart ad and then once the good Odin has finished we can decrease the discount that unless this counter
goes to zero.
This function will block this main function will be blocked in a blocked mode and then I'm going to
pass WG here.
I have to pass the reference to this weird group not element itself.
So now almost all functionality is good is completed.
The last thing we need to do is we need to add a V8 function here.
WG don't wait.
So this function will check if this cone that has time to zero or not.
So here we are incrementing going to the Goddard in finishes we are looking and encounter.
So this works for all the hundreds of iterations.
So when the iterations of it unless this way W G dot weight determines that this counter has returned
to zero it will be no blocking more than it won't close this main function so it we'll wait here.
So if you run it again
there is some problem here.
Valid memory interests.