pthreads #3: Dynamic Memory

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I'm dr. Fraser and in this video I'm going to show how to use AP thread and pass data to it and from it using dynamically allocated memory so we're doing this all in C so we're going to use malloc as a starting point I'm using the code that we ended with in the first video on creating a single P thread and we were passing in this example we passed the value in as a pointer to a variable from my main because I came in here as my argument pointer and then I returned the value not as using this peak here at exit but instead using this global variable sum so create a difficult we'll variable some wrote the answer into it and at the end I could access that so I need to have using a global variable you might be thinking that's not the best way to go and indeed as we pass around pointers when we start to use dynamically allocated memory it can sometimes be cleaner so let's start cleaning that up so the first thing is let's work on passing in dynamically allocated memory into the function so here I am accepting a pointer it's a pointer to avoid and it basically be anything now we're gonna make it to be a pointer to a long long which is the numbers we want to count out them to so the code I have here would work as before but I can actually kind of shorten it down a little bit well I should I let me find I'm gonna I cast my pointer so I cast it from a long long a part of me from a void star into a long long star and then I grab the value and put it in limit here now if this was dynamically allocated previously I'm gonna free it here and I'm done with it so I'm gonna do free I can free argh I can also free the limit PTR because of the same thing so I could for you this one or this one singing to use it and then clear it and now that I'm here at Luminas will also allocate some space for the answer so I've got this variable sum let's move it inside make it a local variable which is always a good idea and now I want to pass back the answer and I could have dynamically allocated to start with I'm just gonna keep it very distinct very separate so I'm gonna say there's a long long let's call it answer and it's gonna be a pointer equals well I want this to be dynamically allocated so I have to malloc now the easy way to do malloc is to say I want this to be the size of star answer so I'm gonna dereference answer now you might be thinking this isn't going to be safe because I am dereferencing something that hasn't yet been created maybe it's null what happens in you dereference a null pointer bad things do but sighs it doesn't actually do on that run time do you referencing my pointer what this says is I'm gonna give you the number of bytes that this would have been it can then figure that out from the compile time information so this doesn't cause any errors this is actually a nice flexible way because if later I decide that well answer should actually be a double for example if I changed it to double here this size it would automatically recompute for me get rid of the wrong comment I guess I here pass back data and dynamically allocated memory so now I've dynamically allocated the memory I need to actually set the value in here so I can say start yeah start answer so dereference answer equals sum so I copy the value in and then I can use pthread exit to pass the value back let's have a look at what that looks like so let's do men on P thread exit it tells us in the P thread dot H file what you already have included which is good and we pass it a pointer so the fun when the function terminates and calling thread it returns a value via that ret Val and here read val is a void star so we pass it in a void star basically anything pointer we pass it will automatically convert to a void star so I'm gonna pass it an answer so we are now giving out a pointer to this memory that we have allocated so it's not likely going to be a memory leak as long as somebody else calls free on it ok so I think that's all the details we want to change here on the some runner at the moment let's go down and have a look at the actual implementation of our function or our calling in it so in here we're currently passing in a pointer to a local variable let's change that to be a pointer to some dynamically allocated memory so long long let's call this one well yeah stick with limit but let's come at this out so we don't have a conflict star limit so pointer to the limit equals well we want to call a new space so we call malloc and this is going to be the size of star limit so follow the pointer just as we did mentioned previously okay so now I've got my space allocated I can say star limit equals and I'll just take this code from before and extract using ASCII 2 long long of the first prim of the second parameter which will be the actual number passed in so now that I have my allocated space I can pass this in to Peter to create I no longer need to pass the address of this because it is already a pointer so I can now just convert it incidentally pthread create would be quite happy to accept the address of that we did then be casting it up here I would just have some errors because I'd be passing in a pointer to a pointer and then casting it back to a pointer and treating the number of something else so these casts can become a problem you want to be very careful that you've got all the data types casting correctly ok so let's double check what we've got we create a new thread of the sum runner and we pass in a pointer here this is a pointer to some new space which is good so we've allocated it here it goes in now when it comes out how does the actual value come out well if I look at Pietra join we can see here that it's going to well we pass in the P thread underscore T so this is the thread ID so we're passing in the thread ID which is good and then I was passing in a null but instead I can pass in a pointer to a pointer a void star star and in fact I don't want to be a void star star I want it to be a pointer to a pointer of the appropriate data type I know it's going to be a long long so let's go long long I'm gonna make this a pointer and I'm gonna say this is the result so I'm gonna have a pointer to the result I need to be able to hold a new value in that pointer and so I'm going to pass in the address of result so this says give me the address of a pointer and when you get the address of a pointer you have created a pointer to a pointer so I'm passing in a long-long star star instead of my void star star so here I've got the the value is now going to be passed back what am i passing back I'm passing back a pointer so up here I pass back a pointer to a long long so that now gets stored here in result and so inside a result I can check that value so I can print star result print it to the screen and then the last thing I need to do is I need to free the memory so free on result because I now elect it I need to free it so let's build that see what happens well it's not happy yet so incompatible pointer type so passing argument to incompatible pointer type let's just make sure we've got this so we've got a void star star we're expecting to pass in I have a pointer here and I'm passing in the address of that pointer okay so it's not happy to can do a conversion to void star star so I can do this in a couple steps I could for example convert this right here and do I avoid star star cast let's try that okay compile that's happy it always makes me nervous casting something to or from a void star star so be careful with that and here I can do some and let's do something simple we know for example 3z v6 it worked great 621 okay so it's looking like it's working okay I can pass in a big number here and it's going to do the usual lengthy computation to come up with the answers for me so that's looking pretty good now you can ask how could I test this well I could set up this whole thing in a loop and run it a bunch of times and check how much memory is being used on using the commands top so I could see how much is being executed that way I could also run this through a number of different utilities that will check for memory leaks let's beyond the scope of this video but a tool such as Val grind or a number of other ones will do very extensive memory checking to make sure that all allocated memory is correctly freed so what have we seen in this video we saw a game that we can call a thread function but in this case we passed in some dynamically allocated memory this was a point to do some dynamically allocated memory which we then used and once we'd extracted the value and sorted as a local variable I called free on this and then do my usual processing inside of my thread and then when I was done I allocated some new space for the answer and passed that back so we returned the pointer using P thread exit down here when we wanted to call it we called malloc so we allocated some space for the argument going in and set up that value and then pass it in here as the final argument to pthread create and then when I call P thread joined this is how I actually get the value that was passed in to P thread exit so I pass in the second parameter here is a pointer to a pointer to my value so here it's a void star star casted - so it actually accepts that the compile is happy and I pass in the address of my pointer in order to get the pointer to the pointer I can then print it out and then finally I can free that free the pointer and that will free the memory that was allocated all right thank you very much
Info
Channel: Brian Fraser
Views: 32,059
Rating: 4.9717646 out of 5
Keywords: POSIX Threads, Software (Industry), GNU/Linux (Operating System)
Id: KVF4kYvd7e8
Channel Id: undefined
Length: 10min 39sec (639 seconds)
Published: Tue Jan 27 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.