pthreads #1: Introduction

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 pthreads to sum up some numbers basically a simple task this is going to be a sequence of videos in this first video we're going to demonstrate how to pass a value in as a pointer but return an answer using global data in later videos I will show how to use it pthreads with pointers going in and out so to start with let's get the kind of the context here I've got a program kind of partially written here that it will accept some arguments in the main and if you've got the wrong number of arguments it will simply tell you got a problem so I will just simply run my make and then we'll run that program tells me there's a problem if I say I want to some numbers here it doesn't do anything at the moment so what we're going to do is we're going to make it add numbers between 0 and in this case 3 and we're gonna do that on a background thread so the first thing we're going to want to do is get this the arguments extracted so the first thing I'll do is I'm going to use long Long's so that I can have very long numbers to demonstrate how long processing can take on a background thread and let's call this one limit and we're gonna pull this out of using ASCII 2 long long which looks like a toll and this is going to be our V sub 1 so now I've got the actual number I want to to work with I know need to figure out how to make this sort of happen on a background thread so I'm going to start by writing the code that does the summation first to understand what that function is going to look like let's have a look at the function that's going to create the thread so I'm going to man pthread create and that's going to show me the help text on what's going on here the interesting part here is we pass in a 2 this function pthread create we pass in some pointers to thread datatype we pass in some attributes to start it with but critically here we're going to pass in a pointer to a function so a function pointer is C is basically saying we're going to run this as being our thread function he was gonna be the where we start executing the thread from and we can see the datatype here it is going to pass return a void star so it returns us a pointer and it accepts a void star so in C void star means basically pointer to anything avoid being nothing but it's a pointer basically to anything here so we're gonna name it whatever we like and pass in a pointer to our pthread create function and the function that we're gonna create has to have a void star return type and I'm gonna call this one some runner-runner around the bowl these sorts of things indicate sort of it being a thread and so I'm gonna put in here void star and let's call it argh so I've now got my function here I'm gonna put a comment on it just so we can follow along what it's going to do this is going to be the thread function to generate some of 0 to n where n is the number we're looking for so it may not be clear at the moment but this arg is going to be we're gonna make it a pointer to a long-long we'll just make sure that inside of main when we set up this pointer will make sure it's the right type so long long and I'm gonna split it up into a couple steps so I'm gonna call this one limit PTR so this is gonna be my point limit to the pointer and I'm going to do a cast I don't really need to send you this a pointer and I'm gonna make this a long long pointer and it's arc so I'm typecasting a void star to a long long star now if I wanted to I can go long long limit equals and I can dereference limit PTR that's maybe a good way to walk through what the pointers gonna do for me I would normally not do this in the two steps but it's good enough in the moment now I want to sum all values between 0 and n in this case my limit so I'm gonna do first come up with some so a long long sum equals 0 and then 4 and then we go into I but I'm gonna go long long I equals 0 I less than my limit effect I wanna do less than or equal to my limit I plus plus some plus equals my that's the basics there in C unless you're using C 99 or like a new extensions you can't declare a variable in a for loop here so I am I'll show you my make file in my make file and I'm running here I am passing in the - STD in space or equals c 99 flag to cause it to allow me to do use just this okay so now I've got my answer here and I'm just gonna put what to do with the answer that's something we got to figure out something to do on that okay so this is starting to look okay I've got a function that we looked up what the type should be the prototype we've implemented that we said well we're going to make sure that well it gets passed in as a pointer which is my void star we're gonna make sure we can extract the data from that once I've extracted it correctly I'm going to run my function here okay so let's go back down to my main and let's start writing the actual code to execute this thread so if we look at Peter to create we have to pass it in a pthread datatype and so this is going to be my thread ID usually so I'm going to call it put it credit here P so thread ID P thread type def and this call us tid thread ID now the next parameter here is some attributes we can look up what those are basically it controls what the thread is going to how it's going to function so create attributes this is going to be a type P thread underscore HTR underscore t and i'm gonna call it h t TR so actually creating that and then there's a special function that will initialize this data structure so pthread attr underscore in it and i pass it in a pointer to my data or my variable so a GTR I'll pass it the address of that so this will create the attributes I've already got the thread ID there that's so now I can actually make this happen so I can call P thread create which does the real work here for me I'm going to pass it in the address of my thread ID so that I can track which thread it is that I'm working on I then pass it in the address of my attributes for it to pull that out and then I pass it in the function that I wish to execute which was my some runner some underscore runner I don't put any brackets here if I did I would be executing this function and the return value would be passed into pthread create I want to pass a function pointer so I don't put any bracket at the end and now it's a function pointer and then finally I have here the arguments I have to pass it some arguments in so we see over here we're passing in a void star arc so I need to pass this in a pointer to the appropriate data well I've already got that limit up above right here I pull the limit out of my command-line arguments let's let's pass that in I can't just pass in limit because limit is a long long and I need some sort of pointer so I'm going to pass it in a reference or it's part of me the address of that variable so now we're getting close if I simply run at the moment the main is going to come through do all this work create this launch the thread and then exit immediately we don't want to exit immediately we want to wait until the thread is done so wait until thread is done its work so here I'm going to use P thread joined much like functions for your processes where you call join and I passing the thread ID I don't want to do anything with the return value at the moment so I'm going to use null and which is to say I don't want to capture it so that's getting there now this is gonna wait I said I didn't want to grab the answer from no I'm not gonna return it this way I'm gonna use a different way to pass back data from my point now let me just stretch this out a little bit so we get a little more on the screen there you go so up here I need to somehow pass back the answer we're in a threats we're gonna have two threads one thread which is the main thread starts here continues execution until it hits the weight holds here I think finishes and quits the program my second thread spawned by pthread create comes into my some runner execute down to here it's all going to be inside the same address space so either thread can access the same global data so let's it demonstrate that by creating this we're gonna put our answer so I'm actually just going to move that local variable called sum and we're gonna put it up here as a global variable so some created or computed by the background thread and because up here I am now computing the sum I can now use some way down here at the very end so after we're done I can printf sum is % lld for long long show in decimal and the sum so I now have the answer is so maybe I put a note in here some is a global variable so other threads can access and the last thing I need to do is I need to say well my thread needs to finish and these a good way to do that is P thread exit now I put a 0 because I'm not actually gonna be returning anything this value whatever I put into the exit gets passed back here if this was non null I'm basically not using it so I pass back a 0 here just sort of a null pointer and down here the null means Peter had joined a smart enough to say look you passed me an OL so I'm not actually gonna catch the answer ok so let's see where we're at so I'm gonna come back over here into my name I'm gonna make clean like I'll make so I got a number of Eros we can start to walk this through I'm gonna actually go back into eclipse I'm gonna control B to build so that I actually get all of the errors in here much faster so let's pull those up so we have a lot of implicit declarations anytime you get an implicit declaration warning it would normally be but I've turned on all treat warnings as errors that means that you base you probably forgot to hash error hash include a appropriate file so as it turns out that's exactly the problem so I need a hash include pthread I know I need to be included p3h because I can come back over here man pthread create and it'll tell me up here at the top I need to hash include' p3h so let's go back in here save build and now we're looking pretty good except not quite good enough I get here's some undefined references to p3 create and pthread join now I have hash included the right files but these are actually coming from the linker so this is the error LD LD is my linker so the linker returned a one exit status meaning it failed now what's going on here is it's trying to use the functions I didn't write them I didn't code them and they're not linked by default so I have to go into my make file and I have two here and the pthread parameter and this will say that I'm going to now link the pthread library so this will link in the appropriate functions go back into my code build check that one error log here nothing we're looking good which means that it correctly built my file so I had out here of my some on threads some on threads let's just test it with a three some is six that looks good four five and so forth so now I can put in big and now it's going to compute for quite a while I have here the monitor it's a system monitor under Ubuntu and we can see that my processor is maxed out I have four processors and only one of them is maxed out now one of the big values of having multi threads and multiple threads in your application is that you can begin to exploit the multiple processors in your system I'm running instead of a virtual machine here I've mapped four processors over to Linux so it says we see here not not really able to use the rest you'll note that occasionally Linux will switch processors so it was on my orange CPU one for a while and then switched over to CPU two and is maxing that out for quite a while here I'm not quite sure how long that'll take I'm going to ctrl C to kill it let's go back and make this run a bit faster hey let's keep adding a bit longer to it until we get some number that takes a while but not forever so we can see here the processor sets to max out and fairly shortly it should drop down I'll leave that for a moment so the interesting thing about running this program is after this pthread create and between the joint I could do a fair amount of interesting work so I could do other stuff here so for well we can see just passing that now my answers been computed and here's my fairly large answer it turned out to be too big because it overflowed so I'll keep that in mind for the next time we're on that so I can do this other interesting stuff between the p3 to create the P thread join I could for example spawn other threads to do the work do some other work or I could do some other interesting computation I can interact with the user there's so many different things here all that I could do and I would then possibly be running on a different processor or at least seeming to run concurrently with the background work if I wanted to build incidentally just straight from the command line I'll show that here make clean and I wanted to run GCC yeah GCC on this file I say well I want to compile some on see I need to use STD equals c99 because I didn't use the variable declaration inside the for-loop I need to pass in P thread so that it will actually link in the appropriate library and I'm going to go stash oh and let's call this one manual and well then compiled manual and let's put it in here just to show it does round so if you're looking that would be the command line if you're not using a complex make file okay thank you very much for watching there'll be a couple other videos following this that show how to use threads by passing information around in alternative manners
Info
Channel: Brian Fraser
Views: 229,667
Rating: 4.8994308 out of 5
Keywords: GNU/Linux (Operating System), POSIX Threads, Software (Industry)
Id: ynCc-v0K-do
Channel Id: undefined
Length: 16min 8sec (968 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.