 Hey everybody this is Brian and welcome to the 28th Qtutorial with C++ and we're going to dive a little deeper into not just Qt but computers themselves and today we're going to be covering threads and this has been requested by a few people and yes we are finally here so let's just dive right in go file new project and let's make a console application call it my thread wherever you normally put your projects and finish all right first thing we want to do is create a new class so C++ C++ class and we'll call it my thread and we're just going to leave out any of the other information because I want to explain this as we go so just hit finish and then we've got the my thread class to add or includes oops include q core all right and now we want to actually oops public q thread and what we're doing is we're taking this class and we're inheriting q thread and q thread has a public function called run and you see we are over writing that function and what this is is when you start the thread this is the function that's going to be called run so let's go into our implementation here let's actually make our includes we're going to include Qt bug and let's say void whoops get rid of that bookmark my thread and run all right now we've got our code and this is what's going to be triggered when the thread is run I should say when the thread is started sorry this may sound confusing but we're going to explain this really quick here all right slip into our main and we are going to include that class my thread all right and we will say my thread m thread now that we have an instance of that thread we need to start that thread so say m thread and you see there is a priority and you can inherit priority and we're going to go over priority in a little bit here but for now just do m thread start compile and run and you see it says running so it did exactly what we wanted it to do now as you guessed it gets a little more complex than that we need to explain a few things here so I'll start task manager if you're on windows this is probably pretty familiar here are the processes every application is a process and every process can have multiple threads and you see that each process is using a certain amount of memory well each thread as you guess uses a certain amount of memory and you can take these and you can set the priority real time high above low blah blah blah basically what you're doing is saying that the the cpu is going through and it's giving each one of these processes a slice of time to work with if you increase the priority you're saying okay you get a bigger chunk of the available time that's really all it is and if you're sitting here going well that seemed incredibly simple well you're right qt makes it incredibly simple to work with threads it's very easy you you simply create a class inherit from q thread and you override the run function and then you can do anything you want to do now let's expand this class a little bit because I think this example is a little too easy so what we're going to do here is say q string name and we're going to have the ability to name our thread so let's just save that go into here and we're going to call this m thread one and we'll say m thread one that way it now has a name and we're going to make a couple of these say two two two and let's say three three and three and let's grab this we now have three threads now each thread is going to run the same code each time you fire up this thread it's going to call this right here so what good are threads well if you're running an application and you want to do something that's going to take a very long time like let's say you want to count to a trillion but you don't want your application to lock up you want it to be able to do other things that's what a thread is you say here go do this task go away and the thread goes into the background does its task and then you can query it later and say what's your status so what we're going to do here is we're going to say this name and let's just save it fire this up and sure enough thread one two and three all ran now let's get a little more complex here and let's just say four int oops help if I could spell for four int i equals zero i less than and let's just say a thousand i plus plus in the words we're going to increment i and we are going to just simply print out the thread along with the number and this will show you the real power of threads and it's going to look like a big blur but you'll understand it once you see it run and it went very quickly because I have a pretty pretty hefty computer we didn't count very high but you can see here thread two nine nine eight thread one nine nine nine thread three nine eight nine so as you see these are all running at the same time they're all counting let's expand this number out uh much larger here let's run this you can see it's counting and you can see that each thread is running independently I don't know if the video is picking it up fast enough but I can see two one three and you see the numbers just incrementing incrementing incrementing so that's how threads work you can run multiple threads at a time that is the bare bomb basics of how the queue thread and how threads in general work we're going to cover this over some more videos because this actually gets a little more complicated especially when you start accessing variables you need things that are called locks and I'm running out of time in this video so we just simply won't cover it in this one but this is Brian thank you for watching I hope you found this video educational and entertaining and uh stay tuned