Threads In C++ An Introduction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys this is reparation and watching CV peanuts video series in C++ and today's topic is introduction to the threading in C++ or C++ 11 because this threading which is this thread header was not there before C++ 11 so if you want to practice this you must have to have this C++ 11 okay so that was first thing second thing is this is not related to the OS threading I mean how always take care of threadings and how they are actually managed in processors how they say are the addresses and all that this is really not about that this video is about what do you understand by threading and give some example in C++ so this is actually a C++ threading I mean how you create threads in C++ and what benefits you get if you use that so the answer for this question is not that simple but I have tried to summarize it like in every application there is in default thread which is main thread okay so whenever you write some program like this you have some main function and from from here your program starts and it ends here so whatever you write in between if it is not threaded application it will be a single threaded application because mean itself is a single thread I mean this function is not threading I mean you can tell this function a single thread obviously because OS runs this function and if there is no thread this is the only thread okay so that is the only thread if you don't have any thread but if you want to create threads you have to create that inside this main thread okay so I will explain what is threading I cannot get the actual sentence how to explain that I will explain with the diagram and calling structure so wait for some time and lastly we will see an example that you really get the benefit of using thread [Music] oh why did my Alexa wake up sorry Alexa I have to stop you so where I was yeah I was talking about lastly you will get the actual use case why you should use thread if you can okay we'll see the benefit your full program will run in half of the time how much it was taking before if you are not using thread so let's move to the next point a thread is also known as a lightweight process so what is process when you create an application or in hello world world if you create a simple program like this this is an application and a process when you run this this becomes process inside your operating system and if you have not used any multi processing or multi-threading stuff inside this this is just mean one single process okay so a thread is also a lightweight process what is lightweight actually process itself is a little bit heavier because it has to have so many things but if you are creating a thread inside a process that thread is little bit lighter than the process because you don't have to manage different different stuff because threads actually run inside process okay so you can assume that process as a big entity and threads inside that smaller smaller entities and the idea is to achieve parallelism by dividing a process into multiple threads if you can create multiple threads I would say you should create and for the examples I have written few lines here the browser has multiple tabs that can be different threads so you see so many tabs right in browser they are different different threads if you will open different different tabs they all are handled by threads okay so second point is ms word must be using multiple threads one thread for formatting the text another thread for processing inputs so you might have used ms word or something any editor for sake and you would have noticed that whenever you type something it formats your text in some are whatever order you have selected so that is nothing but a thread which is running in the background and it is taking the input whatever you are writing and formatting it in some way or other and another example is which is a very good example I used to give this example when I was giving interviews this example is spell checker you write something and it will pop up like oh you didn't write it correctly so how bad happens because you didn't press any button then who recognized that you're writing something wrong or right so it's thread which is running in the background and as soon as you write something and hit space it will check that particular word and find in the dictionary whatever it maintains whether it is correct word or not so that is a good example of threading so third point is Visual Studio code editor would be using threading for auto completing the code or it's called intelligence so if you don't know what is intelligence let me give you the example so as soon as I start typing see these options are coming right like am I supposed to write I and C in clear intellisense introduction so these things are coming from some thread the thread is taking my inputs whatever I am writing here and it is matching with its database if it is able to find something it is giving me the options here that whether I will be liking this one this one this one so like this okay so this is also a thread running in the background okay then so this was about threading how it actually looks like okay so you might have understood how thread actually works I mean in the lamin language but we'll see the example concrete example in a couple of minutes now we used to create threads in C++ actually this topic is another topic I mean I'll create this topic after this video there are actually five ways you can go in C++ if you are creating threads so function pointer is the point we'll be looking here but rest of these we'll see in the separate video because everything has its own syntax and that is really very important so so far so good now let us discuss the requirement what is the requirement here so that we can create a program so find the addition of all the odd numbers from 1 to some number this is a very good number actually it took so much time for me to find this number because it was not easy to find some number which is giving you the expected output what you want to show okay and all even numbers from one train so basically what I'm trying to do is find the sum of all the elements from start to end okay so even some an odds-on and lastly I'm just printing there so these are the functions this is start and this is and okay and this you you I mean you ll is nothing but unsigned long and long I have type def it so that I don't have to write this again and again so you can see highlighted places I have to write these many places unsigned long long I would have grown old okay now as you can see this is fairly simple program and you know the working of this program I know that really very well you will call this function it will be called with these two parameters it will come here it will do the job I mean it will do the sum come back another function will be called it will do its job and then it will come back and you will do this so can you see a thread here it is single thread let me give you an example here so your program started from here it saw that okay I have to run find odd function okay it went to that find odd function completed that came back to this find even function okay it has to go and execute that also so it went and executed that also but it came back again and then it executed these two and returned so can you see this there is only one thread there is nothing like it is running from here to here and something else is also going on somewhere in the background no nothing is going on in the background so this is kind of a single thread there is no parallel processing here or multi-threading here so let's quickly run this program and see how it works so this is the command for compiling this because I am you threading here so you have to write this P thread and I study C++ 11 this is very important point because you cannot have this threading until unless you compile it with C++ 11 so I have not written anak threading but still I will compile like this it's ok no problem so I have compiled it let's execute that okay so odd sum is this much and the one okay so you would have noticed that it took quite amount of time for calculating this that's what I needed now if you want to know how much time it took we can calculate that so wait a minute I will use something like this so this is going to be your start time okay so what we are doing is we are taking this start time when we actually want to measure the work here after that also we are doing the same thing and start time doing the difference and this is the difference so let's print out the difference as well so that printing will go here okay so this duration is the result how much time it was taken and count and this is some number which will give you the seconds because this will give you maybe in microseconds so converting that into some seconds okay I'm not that good in maths so leave it so this will give you the seconds so let me just write that seconds okay we will compile this again to run this now it will give you the time it took to actually complete this calculation so it is taking four seconds okay good so I assumed two seconds for this one and two seconds for this one I mean that's what it would have done so rohtul four seconds so now keeping this as a benchmark we will see how we improve if we use thread so let's implement the thread now now concentrate here this is the syntax now I'm going to explain the syntax so now open up your eyes thread you have to write thread and t1 this is the name of your thread and you are done but thread needs a callable object that's what I said you need to have these things inside thread so that thread will run them so as I said will be using function pointers so I'm going to give function 100 here so first I will give find even and this start and end ok and similarly add t2 and this time find odd start and ok so this is the send text for a function pointer so if you will create a thread this is thready you have created it as soon as compiler ah sorry operating system will run this particular line it will create a thread for you and that thread will run this function so threads needs functions callable stuffs like functor or lambda function or static or dynamic sorry not dynamic static member functions are not static member functions because it has to run something right so that's how it works and after defining what it should run you have to give the parameters so this is the parameter section you can give as many as you want there are a few points like if you want to pass these parameters as reference in the thread you have to use some STD ref so we are not going to discuss all those things and the same thing goes with this one now as I said as soon as this is hate thread is created now there is something called joining the thread T 1 dot join I won't talk about this join much because it will take so much time actually not so much time but I decided to make another video for this but for now you just understand that if you will not write this join these threads will not be joined to your main thread and they will be running and if your main thread goes off I mean if it is completed but your threads are not completed then there is no way to get the results of these threads ok because your main thread has gone so you have to join these thread with your main thread that's why you are writing P 1 dot join and T 2 dot join so what happens when you write this actually when your OS is creating it will start running this function okay so it will actually call this function and come back and this function is keep on running and meanwhile it is back and it will create another thread so this will result into running this function and this will also come back I mean there is no single thread now I will draw the diagram don't worry about that and they are continuously running and it will come I mean your program controller will come back and it will run this T 1 dot join now you won't move further you will wait for the T 1 to complete to go further ok and let me comment these two functions because now we are going to test these functions with thread okay it's that simple threads are created now you are waiting for those threads and they are running in the parallel now there is no calling like find odd will be completed and it will come back and then it will call find even know they both are running in the parallel thread ok and we are waiting for them here so let's see what is the output now I will create a diagram don't worry so let me run this compiled successfully run this and now notice the timing here it took 2 seconds same program we didn't do anything we just ran this find even and find odd in two different threads and as I said maybe this one is taking 2 seconds and this one is taking 2 seconds that's why we had to complete this one and come back and this one that's why it was 4 seconds but this time we were running this one and this one parallely I mean these two functions parallely so it took 2 seconds only you can understand that right and if you still find it difficult to understand let me give you an example with some diagram so as I said when you will hit this line I mean when your OS will hit this line it will create a thread so instead of going to this function and waiting for this to complete it will go to that function and let's assume there is this find what it is even so there is this find even function and this is the function so what this thread will do okay you start so this function is actually running and it will come back while it is running okay it is occupying the processor and doing its job and your main thread is actually back and it will execute this one so this is also another thread calling so this will go to find odd function and let's suppose this is that function so it will start running this function so this function is also running and it will come back okay and this is also running and this is also running while this thread is doing all these stuff so it created this one first so it was actually running when it created this one set so this one is also running now it came back and now this one was executed now as soon as it hits this join it will know that okay now I have to wait for this t1 to complete to move forward so your main thread is waiting for this find number to come back okay so actually it was running and it took maybe some two seconds and it came back and you have to wait for t2 also so this also took two seconds but as they were running parallely both took two seconds together it's not two plus two because they were running Palelei so t2 has come so now you can move forward check the timings do all these stuff and you got the result so this is how it works you remember before you actually go to that function in this case you actually go to that function and be there till that is finished and you will be back I mean your control will be back to this main function and then you will go for this find event and that will also take two seconds and you will come back and do all these stuff so that's why it was two plus two but now in threading it is running parallel e so that's why it took only two seconds and I think this much is enough to explain how threading so if you liked the video don't forget to hit the like button dude it will help me a lot and if you have any suggestion or something most welcome and don't forget to see the next video which will be coming in maybe one or two days and that is about how many types are there to create threads in C++ so actually there are five types okay and we'll see all the syntaxes and it will be fun so I'll see in the next videos bye bye don't forget to hit the like button guys love you bye bye
Info
Channel: CppNuts
Views: 66,292
Rating: 4.9291835 out of 5
Keywords: introduction to threading in c++, threading in c++, threading in c++11, threading in c++ with example, thread example in c++, thread introduction in c++, threading, programming, c++, multithreading in c++, introduction of threading in c++, basics of threading, threading simple example, threads, operating system, threading in operating system, process in os, threading tutorial in c++
Id: TPVH_coGAQs
Channel Id: undefined
Length: 18min 10sec (1090 seconds)
Published: Sun May 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.