Threads in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's up guys my name is Chen oh welcome back to my c++ series today we're gonna be talking all about threads and in general how we can parallel eyes that's a hard word how we can make stuff happen in parallel because most of the computers or processes or devices that we're programming for nowadays have more than one logical thread of processing that they can actually do so far all the code that we've been writing in the c++ series and the opengl series as well if you guys are watching that has been on a single thread which means that when we actually write code for those episodes and for those series we're literally just making the computer do things one kind of line of code at a time on one instruction at a time which is fine a lot of the things that we've written have obviously not been particularly fast or that they haven't needed to be really like well optimized or really quick because we haven't really written any heavy code any code that has that will actually take the device a long time to compute it's been really simple stuff but as we get into more complex programs it's very beneficial for us to actually move off certain work to different threads of execution not just for the sake of kind of performance but also for the sake of what we can actually do with that for example we've used sed CN get to request input from the user into the console however we can't do anything while we're waiting for input can we we're literally just waiting to receive input and that's kind of like a while true loop that's just going on infinitely and until we press ENTER that's it like our thread is blocked but what if we could maybe do something else like print something to the console or just log something or write a file or whatever while we're actually waiting for user input we still want other things to happen that's one example that's probably really simple to understand but anyway we're just gonna dive right into the code and see what we can actually do alright so the first thing I'm going to do is show you kind of how threads work on what we can do with them so I'm going to include this thread header that we have here which includes the threading support basically into this file so that we can use a thread class and everything and I type in sed thread I'm gonna call it something so I'll call this a work because it's gonna be like our worker thread and then what I mean what are you to do is pass in a function and optionally any kind of arguments that it may need so let's make a function called do work this is going to be the word this is going to be the function that actually performs what we want to happen on another thread of execution so I'm going to pass it into here by just typing and do work if you guys haven't seen my function pointers video definitely check that up because this is how this works it takes in a function pointer that's why we just pass in do work without parentheses or anything like that video link will be up there and as soon as we write this actual code it's going to immediately kick off that thread and it's going to do whatever is in here and it's going to keep running until we wait for it to exit so the way that we can actually wait for something to finish or wait for a thread to complete its work is by tacking is by typing in worker dot join so this is our thread object of course and then this join function is essentially going to just wait for this thread to be joined now thread joining is a another kind of portent potentially complex topic that might be worth discussing in more detail but basically in like in c-sharp and kind of more modern programming it's just called wait or wait for exit or just me away it is probably reasonable basically all of this does is it just says hey can you wait on the current thread for this thread to finish its work so block the current thread until this other thread has completed so because this stuff is running in parallel we have our main thread which starts off a worker thread which does its work and then eventually what we're doing by writing this join kind of call here is we're saying on the main thread wait for that worker thread to finish all of its execution before we continue on was with whatever our main thread hands so what that means is that this CN dot get line of code will not run because it's the next line of code after this it will not run until everything that is in this function has finished and we'll say this in pro and prayer will citizen practice in just a second as well so as an example what I'm going to do now is just let's just dive right into it and I'll demonstrate that example that I made with sand cat if we just write a normal program maybe we want work to be done that prints something to the console we'll say it prints working to council just like that and it's going to kind of keep running forever we essentially wanted to just run forever so I'll put this into here if we were to want to write something like this that performs any kind of work that happens until we tell it to stop by maybe hitting enter how would we go about doing this well we know that what we could do is we could say Manuela Casey and don't get so maybe we want this while loop to continue until saying get actually return something because saying get we'll just wait for us to basically press Enter so we want to write a program that waits until we press ENTER but the line of code that waits until we press Enter blocks execution because it's waiting for us to press ENTER so it can't actually print working continuously because it's waiting for us to press ENTER so on one thread this doesn't really work we need to be able to do two things at the same time we want to be able to wait for the user to press ENTER and repeatedly check to see has the user press ENTER but then we also want to log working to the console so let's look at one way that we might solve this so what I'm going to do is write a static boolean here called s working and or well we'll call it s finished and I'll set it equal to false because we haven't finished yet and then this while loop will run until finished has been set to true so while not finished keep printing out working and then what I'm going to do over here is Pesta D syndicate which blocks this thread until we press ENTER and after we press ENTER I'm on a set finish to true and then I'm gonna go just wait for this work at thread to join so what's happening here if we look at this kind of concurrently and how it's actually running this is printing working as fast as I can to the console and that's all it's doing this thread is waiting for us to press the Enter key and when we do in advances to line 19 which that's finished it true the next time this checks checks the state of finished it will see that it's been set to true which means that I'm finished so it's going to advance past this while loop and at the end of the function thus that thread has finished and this is going to make sure that we don't do see and get until that thread has actually finished its execution so if we just hit f5 and say what this actually does you can see over here that is repeatedly printing walking and when I press ENTER that's it it stops printing working and if I press ENTER again it's going to terminate our program because we have another scene not get over here which waits for that before it just closes our program sorry maybe instead of printing working as fast as possible we might want to say well actually let's wait for a bit the thing with multi-threading is that if you continually do things as fast as possible like this it is going to kind of result in like a hundred percent CPU usage for that thread which isn't great so what we can do is because they will wait for a bit and what I'm going to do here is just tell this current thread to sleep so I'm going to write asleep before and then one s which stands for one second and in order to write code like that I'm gonna have to using namespace Pro know literals okay so basically I'm just saying after you print this working line of code just sleep for a second before you go back up to this while loop and continue printing working until finished is set to true so if I hit f5 now we should get working printing to the console once every second and there we go you can see that it's ticking like that working working and if I press ENTER then that's it it's done and in fact let's just maybe write something to just indicate that it's done so after work adjoin I'll print finished and let's see what that looks like so there we go working working working ENTER and you can see we've finished and that is the end of our execution so that's a very basic example of how threads work arguably fairly useless but the point of this video is just to show you first of all how you can access threads in the C++ API of course this ends up boiling down to platform specific code but you just need to include thread and when you just make a thread object by typing in a city thread and then you make a variable out of it and you pass in a function to the constructor that immediately kicks off that thread doing whatever work you've made it to and that thread will continue until it's done you can always use like that thread join to actually wait for the current thread that's kicked off that second thread to actually finish its execution in case you need to do some kind of clean up or wait like this for the actual program to terminate after all the threads are done that's as far as the API goes there's really not a lot of things you can do there's not a lot of things you need to do the concept of threads is really really simple but it gets really complicated really quickly like a lot of things do in C++ the sed this thread thing as well can just make sure well there's also a this thread object that I used here if I just grab my laptop again we have this sed this thread which you can use to actually basically give commands to the current thread because you can say that in this thread I don't have access to this thread object even though I'm actually on it so what I could do for example is just print the thread ID that I'm on so I might say something like s CDCR started thread ID equals and then I'll print the actual thread ID by typing in a city this thread get ID and then I'll copy that line of code and also print it maybe after we finish over here and these authorities should be different because they're running on different threads so if I hit f5 to run my program you can see we start at thread ID 8 3 4 8 and then if I hit enter you can see it's a completely different ID because they're actually on different threads of execution we've created another thread to print our working stuff to the console so yeah threads are really important we're going to be talking a lot more about them in the future they're really useful for speeding up your program the primary purpose of threads is optimisation but not only you can also use them to do things like we did today that isn't really possible to do because we literally need to be able to do two things at once if you did have your own send dog gate function you could of course alternate between work what I mean by that is let's see if the users press ENTER if not let's print working to the console then we do that again like you can do it on one thread you can't with seen yet because it blocks the entire thread but if you had your own method there's no reason why you couldn't do that so it's not like things are completely impossible it's just more or less than some things a lot easier and a lot faster if you actually utilize threading and that's a huge topic which we will continue to explore in the future do you guys like this video you can hit the like button you can also help support this series by go to patreon.com/scishow no link will be in the description huge thank you as always to all the patron support this series they would not be here without you I'm gonna get out of this forest before it gets too dark and I'll see you guys next time goodbye [Music]
Info
Channel: The Cherno
Views: 204,603
Rating: 4.9570241 out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, threads
Id: wXBcwHwIt_I
Channel Id: undefined
Length: 11min 34sec (694 seconds)
Published: Sun Mar 11 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.