C Programming on Raspberry Pi - pthread

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in the previous video we talked about how to create two different processes on a Raspberry Pi in a C programming language and actually in any Linux operating system it also talked about how to share data between those processes well I'm going in this video I'm going to introduce a other way of having parallel execution of our program and that's going to be through the use of a thread and we're going to make use of the P thread library and the P stand for POSIX which is POSIX is just a set of standards and so it's a standard way of implementing a thread in C programming on a Linux operating system so let's look first of all let's compare some of the similarities and differences between a process and a thread as I said in my previous video a process is when you would create a new process we create a copy of the program and it operates in its own memory space a thread operates within the same memory space and so it is possible to share variables between threads directly in our program rather than having to create a pipe to share data between the different operations I remember I when we created a new process we also then had to create a pipe so we could share data back and forth between the parent and child process we don't necessarily have to do that in a thread but we still have to be able to protect variables from being accessed by two different threads at the same time and we'll use something called a mutex to do that but we'll we'll get into that when we get into the code so the main difference is a process is operates in its own memory space and a thread can share memory space with other threads and I guess the other playing a note is a process can have multiple threads so we could have two processes like I'm showing here each with with their own threads and have a lot of things going all on all at once so let's go ahead and get into some of the code just to demonstrate okay before we actually demonstrate the thread I want to first kind of show you what happens when you try to do a lot of stuff within a single thread and a single process on a raspberry pi now the raspberry pi 2 has four cores so we have the potential to do four things at once but what I say it's only the potential to do four things at once because if you have a program like the very simple one I'm showing here that's a single thread and a single process it's only going to use one core and so it doesn't matter if you've got those four cores this program is only going to execute as fast as one core can allow it and I'll demonstrate that will just compile it and run it actually I think I already have it up here let's close that one first a compile build and actually before I do that just so all this is going to do it's a it's a for loop it's going to iterate through an increment a counter then it's going to close so we're not going to see anything on the screen but watch over here my if my prot my indicator here right now you can see I'm using about at about two percent zero two percent of the CPU capacity and I guess you see it over here as well I just pop it out up use some capacity but you see it's it's hovering around between zero and one or two percent so when I go over here and then I hit run and bring up my process manager you can see this program is called single thread and it's using 25 percent of the CPU well that's 25 percent because we're only using one core this this process is is operating as fast as one core can allow it but it's not making any use of the other three course on on the processor so this is the reason why we want to be able to do the multi processing or multi-threading on the Raspberry Pi - if we have an application where we really need to have use more than one of those cores we need to create a new thread or we need to create a new process okay and so now let's go over and hop over to the program I wrote to demonstrate the P thread first of all we're going to import some include some libraries we have the standard i/o library and a standard library this is the one where we get the P thread so we get the P thread library there we're also going to use this types and the u standard and we're going to use that just like we did in the the fork and the pipe video we're going to use that to get the process ID and what this program is going to do is we're going to create three threads but you'll see that they're all in the same process okay so first thing we're going to do is we're going to declare a function here and this is the function that's called by that we go to in each thread that's created so you need to create one it'll be in it just this kind of you can call it whatever you like but you're going to use this you use the asterisks here it's going to create this this function that's passed to the thread and that's where that thread will execute from from there so we're just going to have one of them for this and next we have we're going to create a mutex and what this mutex does is it it's it's a I think of it as kind of a a flag that can be set and cleared that all the threads have acts - and we used that to control the access to variable global variables of shared variables and we need to do that so that we don't have we don't have more than one thread reading trying to read to read or write at the same time to the same place we need to be able to control that or we'll have we'll have unpredictable results let's say the next thing I have here I'm just creating one global variable is going to be count called counter and each one of our threads is just going to increment that that's what this program is going to do they'll be very simple but just to demonstrate how the threads work next in the main function here we have I'm creating three integers and those are just going to hold the return value when we create each thread okay and then we have to create this PE thread type and I'm going to have a create 3 of them there's going to be thread 1 3 2 and thread 3 so and those are just like they sound we're going to create 3 threads and that's what they're called alright okay so now in this next section I'm going to scroll down here it's a little slow with everything I have running but here we go we're going to create each thread and again we're going to have this this value the return value stored in here and then when we when we create reversal we're going to call this P thread underscore create then we're going to pass the the address of thread 1 2 this will just use null here and this these are user you could pass you can pass arguments to your to your thread if you like but we're not doing that in this case and then then this is the the function that's called we know the where the thread starts its execution okay and and so we're going to do that three times so do that for each one of the threats and you see each of the threads is calling the same function but they're going to be executing in parallel all right so we've created three threads there and let's just jump down to this function okay here's the the function that we're going to to execute each thread you can see we have a very simple for loop here we're going to increment our we're going to loop ten million times yeah as ten million and we're going to lock our mutex and this is how we lock the mutex pthread underscore mutex lock and then pass to it the mutex then we're going to increment that counter and then we will unlock the mutex so that's all this does and then when it completes that each thread will execute this line here and it says counter value is it'll give the value of the counter and it's going to get the the process ID of the current process now as I said we can have multiple threads in the same process you notice we didn't create a new process so we would expect this to return the same value each time but this counter value should be different because each one of the threads is going to increment this this counter ten million times so we should get different values there next let me jump back up here okay after we created those threads or then we're just going to print the process ID of the the main process just you can see that it is the same as the threads and then down here it's a very important to use this use these lines here so you have this P thread join and we and you then you pass to it the thread the thread name now if you don't do this it's possible that yours this main program will just go through and eggs it and your threads won't finish so you need to do this to hold hold the progression of the program at this point until the threads are finished okay so that it won't go past this point until the threads are finished once those threads are finished that it'll proceed down to this line right here so and then we're going to just from the main process again print a print that process ID then we'll exit the program so now watch my watch the process monitor over here and let's just look at this one see we're hovered okay jumped up to 12 because I clicked over there Wow there they were back down to one percent remember it was only went up to 25% you need to Det the other test I'm okay just to compile that and build it I'm getting a warning down there but it's it's just yeah it'll run don't worry about it there we go so there we go got a look look look look we have 70 percent there 80 percent look at our pthread test and you can see that it was we're using it looks like about three-quarters of our processing power so we had three processes and each process was operating on on a core or I'm sorry I'm correct myself we had one we had three threads and they were all executing in parallel and so we were making use of three of the cores of the raspberry pi 2 and you can see from the main this remember this this line didn't get called until after the threads were created but in terms of parallel processes this these threads started to count about the time this executed so that's why we saw this pop up first because it was much quicker to print that than it was to count the 10 million into each process and since each one of these threads was incrementing that value in parallel you can see that the value that I put out was more than ten million so you can you can see that well as expected we counted we incremented by ten million three times and our final value is 30 million and you can see that the values in between are about what it took in processing power to get the next thread going so one thread started counting and then the next and then it was it was counting up while the next thread is created and so on but you can see that on each line this it reports back the same process ID and then we're back in the main function and it prints the process ID right there after all the threads join and so there you have it now that's a very simple example there's a lot more to it but that is the the basics of how to make use of the multiple cores on Raspberry Pi 2 or any really Linux operating system on any computer so that's about it I hope you like this video please subscribe like if you liked it and share it please and we will talk again real soon thank you bye
Info
Channel: SpazzTech
Views: 18,086
Rating: undefined out of 5
Keywords: Tutorial, C (Programming Language), POSIX Threads, Programming Language (Software Genre), Raspberry Pi (Computer), GNU/Linux (Operating System)
Id: i4-jvPYdloc
Channel Id: undefined
Length: 14min 42sec (882 seconds)
Published: Mon Aug 10 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.