Practical example for using threads #1 (Summing numbers from an array)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i want to go over a very simple practical example regarding passing arguments to threads and all the concepts that we learned so far um so instead of having ten threads we're gonna have only two and what i want the program to do is very simple is to sum up all the numbers in this let's say primes array okay and to do this well technically it's not that difficult uh we have to just split it in half right we have half or the first half of the array is gonna be sent to the first thread and the second half to the second thread and then we're gonna get back a value a sum of all those numbers in the main thread and then in the main thread we're gonna just uh sum all the half sums together and we're gonna get the final result that's the basic gist of it so how do we actually implement this well the first step would be to change the number of threads so we're going to have only two threads in this case right and i have only a four from i guess one to two it's not or zero to one it's not gonna be a very long lasting four but it's gonna be there and we still need this emma lock because we're gonna pass some values to it and we need to figure out what we add in here because i is definitely not correct right we want sort of what we are to pass the the start index from which we want to sum half of the array right so i want to the first thread to pass the index to the first element so that's very simple but to the second thread i want to pass the index to the not the second element of course but to the sixth element right so that whenever we we are here we just sum five numbers and then we are here we just sum five numbers and we're done okay that means what i have to do here is say instead of just i and i have to say i times five that means that when i is zero we're just going to get zero which is the first element in array and when i is one right because it only goes up to one um it's gonna give you five which is the sixth element in array so that's perfect okay so that's been taken care of now let's fix up this uh whole mess so we don't need we don't really need the printf anymore right we don't want to print it really we just need to create a sum so i'm going to say here in sum equals 0 and let's create a for loop and i equal 0 i less than well we know we need five elements this is hardcoded you can change it to as many elements as you want if you have an array of 20 you can have here 10 and so on and so forth i plus plus and then sum plus equal primes and then our index but not quite because that index is just the initial index it's going to be always zero for the whole for loop so we want to add this i in the mix as well so if we got index zero where the first thread we get zero plus zero is the first element then zero plus one is the second one and so on and so forth and two note here this i inside this for loop is not at all the same as this one okay so these are two separate places in memory values etc in fact i might as well just change this to it to a j so that you actually know what's going on okay now we have the sum that's amazing but how do we send it back well remember this is returning avoid pointers so what can we do in this situation well there are two options i guess we can emma lock another place in memory and return that while we also free our argument or just return this uh place in memory that we have already dynamically allocated right so this is a really good way of handling uh dynamically allocated memory is by just passing it to the thread and make sure the thread returns it back so that you can then de-allocate it in the main function so now instead of calling free we're actually gonna make use of it after the thread has finished its execution and instead return arc but of course not arc without its value changed because that's just our index and i'm gonna have to say here r equals sum and i'm probably going to have to cast it to a to an int pointer here yeah and now that's going to be correct so the same exact syntax as our above and then you can return arc but with a different value and where do we get that value well we get it right in here in this second parameter which we can define in here we can just say let's say r from result right and we don't have to give it any value because this guy takes in a reference to our pointer right so if you take a look at the signature is a void double pointer so just passing a reference to a pointer and this pointer is going to be set to whatever we return here now that we have return uh our argument well in here we know that we actually have inside r a pointer to the sum itself so we can have here a variable called let's say let's call it global sum initialize it with 0 and just say global sum plus equals the reference of r and let's not forget to also free the memory so a good rule of thumb is that whatever you have on emma lock you should have the same number of free calls right right now we don't have any free calls so that's bad we have like two emma locks one for each eye so i'm gonna call here a free of our r here that has been passed around right so this guy has been allocated here passed through repeated create pass in here and then return back in here inside r and just uh we just read the value from it and then we freed that memory so now if we try to well i guess first we should print at the global sum percent d backslash n and just say global sum and if i try to run this you're gonna notice that global sum is 129 and that is actually correct if you go to google and you try to sum all this up it's just gonna give you 129. not also make sure that everything is right let's do a printf statement in here as well so let's print that local sum percent d vector and sum and if i try to run this we should get two different sums and 28 and 101 2 plus 3 is 5 10 17 and that's 28 that's correct then this is 30 49 72 and 101 okay so these are the correct answers it's it's perfect it's working so far amazing now there we have one warning here i guess we should yeah let's cast this for a double pointer to avoid any warnings that should be fine now if i launch this again yep and there you have it you have your sum computed in a multi-threaded fashion and that's really all there is to this um there are other solutions of course so really instead of returning the local sum we could have just summed it all up in the thread uh here but then we would have needed to use a mutex and uh we have had to have our some global as a global variable so this was i feel like a nicer implementation but definitely you can try and do that and that would be interesting to see and well if you do have any questions leave them down comments below or on our discord server of course the code is going to be found on our website which link to it you're going to see down in the description below thank you so much for watching take care bye you
Info
Channel: CodeVault
Views: 5,216
Rating: 4.9694657 out of 5
Keywords: codevault, c (programming language), threads, sum, array
Id: Adtrk3PREQI
Channel Id: undefined
Length: 9min 41sec (581 seconds)
Published: Thu Dec 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.