Introduction to barriers (pthread_barrier)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i want to cover a very simple concept in trending namely the barrier concept um what a barrier really does is that it usually waits for multiple threads to wait for that object to wait for that barrier object until it lets multiple threads pass that the idea of a barrier is as follows suppose these arrows are just some threads just our two threads and we have here a barrier object so it's literally just a barrier that is going to stop the execution of the threads until there are enough threads waiting at that barrier so if i say that this barrier needs to have three threads waiting uh these two threads once they call the the weight function on that barrier they're just gonna stop they're gonna stop right there because they are not enough we only have two here and we need three once a three a third the thread comes along and uh waits at that barrier the barrier is gonna be lifted right the bear is gonna be lifted and all the threads are able to continue their execution past that point so we're going to take a look at a simple example here i have created some code for simply creating two threads that we then join and what they want is to create a barrier so how do we actually create this barrier object well first things first like any other uh object in the pthread api you have to define here a barrier underscore t and this is going to be our barrier and we have to initialize it like basically any other object we have to give it the reference to the barrier then it's a set of attributes which we're not going to get into those uh we're going to just pass normal here but know that you can customize it somewhat uh using this second argument that you can pass to it but i'm going to just press null telling you that i want just the default plain old barrier nothing nothing fancy and then there's a third argument to this object not unlike uh the mutex and the condition variable where we only have two uh the third one is really this count that we had in uh in here this three that we have in here we pass in as the third uh parameter just like so and similarly we have to destroy the barrier once we're done with it and it's calling here destroy and this guy just takes in the barrier itself that's all there is to creating the barrier now to wait at the barrier what we have to do here this routine is linked to our bitter green so we can use this and then say first things first we can say printf waiting at the barrier right so if we get the message knowing what's up and then we can call the ptread barrier weight and this is exactly the point where all the threads are gonna be waiting on so uh here we have two threads as you might notice that we create and we're creating a barrier of three for free trends okay so in this case after the repeated barrier weight call has returned that means that free threads uh have been able to wait at that barrier and now are executing at the same time right so what we're gonna do here is say printf uh let's say we passed the barrier now hold on this is kind of weird right we have two threads here and the barrier of free house that gonna even do anything because if we launch this you will actually notice that the program never finishes execution you just wait and waits and waits for a thread for a third thread that never comes along right so in this case it will never finish its execution because this both threads have called this function right and only two threads have called that function actually and because of that well since 2 is less than 3 they are still gonna wait okay now if we terminate the program and we change it so that we have free threads instead and we launched this you will notice that now free threads are waiting at the barrier and right after that they all pass that barrier at the same time right so there's not going to be intermingled messages of these two there's gonna be always three of these so three of these and only then there's gonna be three of these past barrier messages and you can do this for as many threads as you want so for example if i have um a multiple of three for example if i have here nine i'm going to change this up to zero to nine well i should get three times the amount of message messages we got before as you can see here we got waiting at the barrier we passed the barrier and then another three threads came along and waited and then another three and so on and so forth of course if you have a 10 tries waiting at that barrier at that point it's not going to be great because the program is never going to finish execution because well nine of the threads passed the barrier as you might count from here and the tenth one did not actually pass the barrier one thing to note here uh the printf messages might be intermingled that's simply because of uh how they got access to the lock inside the print f here that's not really uh to do with what actually happened where we had the them wait three at a time at the barrier so that's that's still working perfectly now the nice thing about it is that a thread can wait multiple times at that same barrier so right now we're having just each try to wait and then print the message and terminate right so we're gonna have to always have that um that multiple of three number of threads but this is not really the way you should use barriers right uh maybe we should have here a for loop or a while loop all right a while of one let's say and where each one of them waits and we can also like sleep for one second so that they don't um execute too fast you know and to not have too many issues what we can also do is have instead of just three we can have let's say seven at the same time so that means that seven of the threads have to come along and actually a waiter that barrier and then only then all those seven threads are gonna continue execution and then another seven have to come along and so on and so forth and i'm gonna also add here another uh slip for one second um simply because i don't want them to print this message and then right next after to print this message then it's gonna become a mess okay so every time it prints something or screen think about it as a sort of operation that it does and same thing here okay so now if i try to launch this with seven you will notice that on the screen i will always get a a set of seven that have passed the barrier at the same time no more no less and this is going to continue forever because it every single one is on a while one loop so this is a nice way to control the execution flow of multiple threads right if you want at some point to have multiple threads together this is the way to do it you just create a barrier you set a limit to it which could be really all your threads if you want to so here we can set it to 10 and maybe even remove the while loop and then you will see that only once all those 10 have waited at the barrier then we actually print anything on the screen and it was a short introduction to barriers i hope you got something out of it if you do have any questions leave them down comments below or on our discord server of course the source code is going to be available on our website at the link below take care bye
Info
Channel: CodeVault
Views: 3,200
Rating: 5 out of 5
Keywords: codevault, c (programming language), barrier, pthread, threads
Id: _P-HYxHsVPc
Channel Id: undefined
Length: 8min 38sec (518 seconds)
Published: Mon Dec 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.