Thread Pools with function pointers in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we have that our thread pull working properly but it was very very simple it was just to a task was literally comprised of just two integrals that they were being summed now i want this task to actually execute a function that can be given by our main thread right so we can give anything to our task therefore gonna actually change this from being a uh a simple two integer tuple to having an actual function pointer in here it's going to be a void point and let's say task function i guess and it's not gonna take in any arguments for now so this is a simple function pointer if you don't know what a function pointer is or how to use it link up top uh you can check the tutorial on that if we have that we can change the main function so that instead of doing this you can say dot task function equals a function that we create right and this can be anything right and uh let's say we want uh sum and product right and we get the reference to it and this function is gonna be created right now on our program like so okay so this is gonna be added to our main function we're submitting this task with this function itself but we also need to change the code that's executing the function right so executing or executing the task itself executing the task itself is no longer just a printf or a result or anything it's literally just calling task function itself and task function is going to be this sum and product for us but it can be really anything so this sum and product has the same example uh as before but now instead of just summing the two numbers we're gonna also get a product of it and say here in a equals rand percent 100 and b equals rand percent 100 in sum equals a plus b in product equals eight times b times b and i'm gonna print up a statement saying that uh sum and product of percent d and percent d is percent d and percent d respectively and the back session and then here i have to pass in a couple arguments i'm gonna have here a new line uh saying a b sum and code all right amazing so this is our function that's being executed being sent to our task and now if we try to launch this we should be seeing this uh result in here and let's see if everything is all right we can indeed see here that 51 plus 5 is 56 and 51 times 5 is actually 255 so it seems like it's working perfectly nice and like before we can simulate it it takes a long time by just doing the use sleep and if we launch this as you can see it did take a bit longer with a threads let's say with 100 it should take even more time as you can see here now one more thing that you might want to have in a function is argument so let's say that instead of a b being generated in here we want to still generate a b inside our main function but the operation is going to be like whatever we want it to be so let's say here we have ink a and int b we no longer have these in here so there's that but then how are we gonna pass in to this function these arguments well first things first we have to change the signature so the signature of this function is gonna be uh two ins inside the parameters here so that's gonna be our new function pointer but besides that we're gonna have to add the arguments themselves so arg 1 and r2 inside this task now and when we're creating these um this task here we're gonna have to add those two uh arguments here arc one equals rand percent 100 arg dot r2 equals rand percent of hundred just like that and lastly we need to change the way we're calling this task because now it has two interest parameters so to pass in these two we're gonna have to take the task and actually pass it as argument one and task argument two just like that now if we try to launch this we should be able to get the same result but we've arguments being passed from the main function while the operation is also like whatever we want it to be now let's actually test this uh this idea that we can have different types of operations and different tasks let's actually split this to this function into two functions and try to work with that how do we do that well i'm going to simply create those two functions just like that very simple functions i think you understand them at the first glance i kept the you sleep here and down here we're gonna have to say here well let's say if i percent 2 is 0 then give us the sum and otherwise i want the product so half of the tasks are going to be summing tasks and half of them are going to be product tasks so now if we try to launch this we should see that we have two types of tasks being run in uh well right now they are all sequentially run by just one thread but if we change that to a four and restart we should have a thread pool that's executing four tasks at a time much faster and we can see that there are all of different types product of zero and twelve is 0 that makes sense 58 plus 90 that's 140 that makes sense so everything seems to be working properly and actually basically all i wanted to cover in this thread pull tutorial one interesting homework i want you guys to actually try to implement is to have the tasks actually return a result so what would be nice is in the main function to be able to get a result from each task how to do that as a hint i'm gonna give you is that you're probably gonna need another uh another cue or another array of tasks that have finished their execution right and probably this is going to have to change this signature here all right i hope you got something out of this video if you do have any questions even down the comments below or on our discord server again the source code will be found down in the description below take care bye
Info
Channel: CodeVault
Views: 1,976
Rating: 5 out of 5
Keywords: codevault, c (programming language), thread, pool, function, pointers
Id: 7i9z4CRYLAE
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Thu Jan 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.