 This threading stuff is hard and it gets confusing, so wouldn't it be great if we could just say hey You know what cute take this function go make it multi-threaded and give me the result back I don't want to mess with any of this That's exactly what cute concurrent does so cute concurrent Provides high-level APIs it makes it possible blah blah blah to Avoid any of the low-level primitives, which is where all of the mistakes happen Let's you just focus on getting the job done now I'm just gonna say it I Love cute. I love absolutely love their technologies. However Every time they make something simpler. They make it infinitely harder to understand and their documentation Not super helpful. So what we're gonna be focusing on is the cute concurrent run and In all fairness This is a highly complex topic and this is not for the faint of heart We're gonna do our best to make it super simple Their documentation is probably about as good as it's ever gonna get But if you're just like a little newbie coming in here You can't really figure out what the heck you're supposed to do or even where you're supposed to start and you're gonna be here for a while So the goal this video is to say hey, let's take one function make it super simple Return a value but make the whole thing threaded So cute concurrent high-level threading threads are complicated We're gonna do a folder counter first thing we need to do is we need to include in our project the concurrent library If we forget to do that we're gonna have a bad time because it's not gonna know what we're trying to do That's usually the first step where people screw up is they just don't include that Now I'm going to At the risk of making things super super confusing make a class And we're gonna call this file scanner This is going to be a Q object. All right I'm just going to put some includes in there Q object Q to bug Q file info Q der Qt concurrent notice. It's Qt concurrent. We're calling a whole library not just a class and Q future so what are these little guys so Qt concurrent is the library actually the framework for concurrency inside of Qt and the Q future This is literally a future value that we're going to get out of the concurrent framework So whenever you see future think of like the movie back to the future where Marty goes off and he does some things But he doesn't worry about it because that's a problem for future Marty So this is future us we're gonna get some sort of future value hence the name future from concurrent So this would be our DeLorean and this would be what we're gonna get out of that Super confusing And we haven't even started yet I'm gonna make a function called scan and right off the bat. You're going to wait a minute We said we're gonna get some sort of value out of this. Well, we can through the magic of signals and slots So we're gonna say scan I'm gonna copy that I'm gonna say signals I want void updated into count and then for private I'm gonna paste that scan back in there, but I'm gonna say perform scan Now signals and slots you typically would not have some sort of return value. Well, we're not working with slots We're working with a function. We want to take this function and make it threaded And then we're gonna say Q int 64 and count Realistically, we should also make this a Q int 64 But I'm going to just put it as an in for now. All right, so we have got Constructor scan and perform scan now. I'm going to copy this I've got some notes off the screen here. So there we go And we're gonna do something threaded and we're gonna do something that could take a long time Q future so instantly as soon as you see the word future You should know that we're doing some sort of threaded operation We're gonna say Qt concurrent and we want to call run and whoa There is a lot of options for us to choose from so really simply we're gonna say Take the current object call a function using a parameter and This function perform scan is Going to return some value, but we're gonna do it in a threaded manner using Qt Qt concurrent run So it's going to be a future value of a type int that gets really really confusing to break it down Q future says in the future. We want an integer our future is going to be Qt go do something make this thing awesome this object This function with this parameter, which is actually this guy right here now. I'm going to Grab this off the screen paste it in Simply put the main thread is not blocked. We can do other things We can do anything we wanted Because Qt is off in the background making this awesome for us However, when we say result equals future result right here will actually if the results not ready yet We'll block until it's ready. So we're going to get the result back from the concurrent thread Seems confusing, but basically get a value in the future using Qt concurrent run call an objects function using a parameter In our case this guy Do some stuff and then when we're ready We want to get that result Man that gets really confusing, but it's actually really easy once you do it a few times And yes, there's a reason why we covered thread pools already because you guessed it In the background this runs on a thread pool. So what's really happening is Qt concurrent Has the global thread pool. It's making a runnable for us It's putting this on the runnable running it and then returning the value It's actually really really awesome There's a lot more to it. It gets really complex under the hood hats off to the people who developed this Or say value zero Honestly, I've peaked at the code. I don't think I could have written something like that It blew my mind when I looked at it Some people out there, especially you see plus plus masters are going to go. Oh, it's super super simple and I'm just like, nope, it's not so if the Got a little ahead of myself, didn't I? so we're gonna See I get to talking and then I can't talk and type at the same time. So qdir path Then I'm going to just say If not Exists Then I'm going to simply return to negative one and say No, if it doesn't exist, I don't want to worry about it And what we can actually do qinfo performing scan on q thread current thread and you'll see that this is a pooled thread We may comment that out because it gets super super chatty q file info This is where I'm probably going to goof up I am not good at this part. So I'm going to say dur Entry info list and I want to say I can never remember this. I think it's qdir I don't want to get the director. I think it's no dot and dot. There it is And we want to make sure that we are counting folders. So And that's a dur is not dur Uh-oh, did I screw something up here? I told you I would So q file info list. Ah, it's not a q file info. It's a q file info list. There it is q file info list list equals dur entry info list and we want to avoid the dot and dot dot If you're wondering what that is when you're in a directory the first item is a dot the second item is a dot dot which means Current and parent and we don't want to like double scan the current and scan upwards So we're going to ignore those and then we want to do only folders That part just messes with my head sometimes and then we're going to say the value equal the list length because we're just getting the length of that q file info list And then for each super simple code q file info info In our list And then we want to say the value We're just going to recursively call this function and we want to say item File path I call that info not item There we go. That should work So basically we're just recursively calling this over and over again If you've ever done this on the main thread or the GUI thread, you know if this could take a long time may make your application freeze and Some operating systems like windows. I'm thinking of specifically We'll you know say hey this application is not responding. Do you want to wait or close it or whatever? That's typically what happens there is when you recursively call a function that's going to take a long time all right, so that is Super simple, but it's also very cool. So we're saying hey This line makes the magic happen This line gets the result back. We can do other things while we're waiting And this simple little function is now threaded running on a thread pool Let's prove that we'll see if we can actually break this thing I'm going to do some copy and pasting q to bug q file info q tech stream q thread and file scanner at this guy I'm going to just because it's very simple code q string get path Really all this is going to do is say open the standard input and then Say give me a path return the value Grab some more code And then q thread current thread set object name main thread so we can see the differences in the threads And then we're just simply giving this path. There's a much more elegant way of doing that. I just put it in there I try to make this code newbie friendly. So if somebody skips A few classes. They're not completely lost trying to figure out what all this stuff does I'm going to make our file scanner and last but not least we're going to say fs Dot scan and we're going to scan that path Let's give it a good build make sure we didn't goof anything up All right, so what's going to happen Program's going to run It's going to say give us a path if we give it garbage It's going to keep saying give us a path over and over once it has a valid path It's going to echo that path out. I should say info that path out Then we're going to make a file scanner file scanner scan And it's going to say hey, you know what this could take a very long time. So I want to do this concurrently Qt concurrent run is going to take this object With this function And a parameter a path And it's going to take this whole thing and shove it off into the thread pool And when it's done Then we will have a future result We can do other things and at our convenience when we're ready. We can say future result and this will block if it's not ready If it's ready, it's just going to hand it back immediately Save run In our path, let's enter some trash Yep, sure works. Let's bring up an actual folder here So this is my video recording folder. This is actually the video recording right now. These are the previous videos I've got like blender cpp some get hub. I mean, these are all of the videos that I've recorded for you to me I mean, this is a pretty sizable directory here. So I'm just going to right click Copy that path And let's go here paste There we go See how that is super super cool So let's scroll up All right scan on q thread main thread bang Main thread is free to do other things and then you see sure enough performing on a pool thread And because this is going so fast and we aren't using other things. It's just reusing that same thread over and over and over again But that's incredibly cool and there's our end result Now You may have noticed right off the bat here We have this signal Because we're using a command line application. This really isn't super helpful for us. It's not really doing a whole lot, right? But what we could do right here if we have a GUI application is we can emit this And now this is super chatty. So I'm going to just comment that out Save and run that again Let's go back and get this path Much faster at least on the screen. It was about the same speed But you can see just how cool that is we can take an entire function throw it out on the q thread pool Let q handle all the details for us and we're just worried about that end result