How to create threads in a loop (pthread_create)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i want to go over a very very simple concept that i see many people actually get wrong and i want to explain it uh here in the beginning so you don't have this issue in the future that is if you have noticed in the previous lesson we had so we had this routine function which is perfectly fine but in the main function we had quite a few quite a few pthread create lines of code and similarly with peter join and as you might notice this sort of duplicated code and if you want more than fortress i say i want eight threads um then i don't want to copy and paste this code again how can i create threads in a for loop well to start off we should actually take a look at these variables p1 p2 p3 and p4 these to work with them in a for loop would be great for them to be in an array so we can create an array instead of all this we can create a let's say th from thread and let's say we just want four threads for now so that's gonna be our array where all the uh thread handles are going to be stored and then we don't want to initialize the mutex multiple times only have that once and then in a for loop here let's get an eye i i equals zero i less than well we just have four threads so far and inside of it what do we actually do well i guess we just copy uh part of this right we just go ahead and copy it and we all we have to do really is to change this so that instead of p1 it says the address of th of i or you could say th plus i that is really the same thing okay so creating the thread has been taken care of we are creating four threads using this simple for loop we can remove these all together that's fine so now that we have created the threads we can just simply call join in the same for loop right so if we copy and paste for example this one and when we format it and instead of p1 here we have to give it the actual value so in this case we gave it the address to where we are going to store the handle but here we can just give you the value which means we just need to say th of i in square brackets and i can return here too because we don't need anything else and that should be that if we remove all this and we try to execute it it looks fine right so if i try to execute this we're gonna get 40 million as a result so that's the expected result because what we have four trends and we have incremented uh this 40 million times looks fine at first glance but let's print a couple messages and see exactly what's going on okay so uh after the thread is created let's print out a simple message on the console saying uh thread percent d has started all right let's say backslash n and then i'm going to give it the i value and then 30 d well we know that after we return from petra the join like with the wait calls with processes we know that the thread has finished its execution so we can see here thread percenty has finished execution and i'm going to still give you the i value here if i try to run this you'll notice a couple interesting things and that is that as you can see thread 0 starts execution that's fine then it finishes execution but only after it finishes execution then thread 1 starts execution and so on and so forth so at no point do we have more than one thread running at the same time that's kind of a bummer because we are really doing everything multi-threaded because we want to have things uh run in parallel but in this case we are not and what's what's the issue what's what's up why is it that whenever we only when we finish the execution of a thread do we get to start the other one that has to do with the way the for loop works here right it's just our logic that we have created here we are well for the for i equals zero we are creating the first thread right and then we are also joining it right so the first thread gets created then it the system waits for that thread to finish its execution using pitra join and then only then the second thread can start its execution because that's just how the for loop goes so this is exactly not the way to create uh threads in a for loop okay so you shouldn't both create or call p thread create and p thread join in the same for loop that way you're gonna have basically sequential execution basically you're gonna only have one thread executing at a time and that would mean you might as well not even use trends the correct way of doing it is to create another for loop after you have created all the threads so run for loop that starts at zero and ends at four so that's the same exact uh definition for the for loop here but we have only the join uh instruction in it now if i try to run this with the messages themselves because well i'm gonna keep them because this is correct right only after we call peter crate we have started the thread and only after we call peter john we have finished their execution if you try to launch this you will notice that all of a sudden things are starting in parallel so uh it says four threads have started their execution so at the same time all four are firing and then more or less the same time or four are being joined are being well they finish their execution okay so this is the way to actually create uh a set of threads in a for loop you have to first create them in a for loop and in a separate one you have to join them don't do the same operation in the same loop because then you don't have multi-threaded programming per se so that we have a for loop that creates an n number of tries at once you can optimize it based on your hardware so my computer has eight cpu cores i might as well create eight threads at the same time not always we have to do this but in this case it's a good uh example i'm gonna just have to change everything to eight and if i run this of course i'm gonna get eight threads and then eight threads have also finished their execution and this number is also 80 million which is the correct result so and uh by the way this message doesn't really mean that the threads finished their execution in this exact order this is just the way we print the message right we go through a for loop so first we print this message then we print the second loop in the third um the in in reality let's say if the fourth thread finished finishes its execution first we're gonna first wait for the first thread so these messages on the terminal correspond to the way we're iterating over the list of threads and we're waiting for them not really the uh order in which they finish their execution and that's all there is to it if you do have any questions leave the down comments below or on our discord server also on our website gonna see the source code for this lesson thank you so much for watching take care bye
Info
Channel: CodeVault
Views: 8,751
Rating: 5 out of 5
Keywords: codevault, c (programming language), create, threads, for loop, while loop, loop, pthread
Id: xoXzp4B8aQk
Channel Id: undefined
Length: 8min 50sec (530 seconds)
Published: Sat Dec 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.