Parallelism vs Concurrency

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
another important topic in multithreading is the difference between parallelism and concurrency so you might have heard these terms maybe interchangeably used but they are not really at all the same let's start with parallelism parallelism really is all about launching multiple threads that execute basically the same code but on a different set of data right so suppose we have here the data which is represented by this box and it's just let's say an array with six elements and let's say we launch six threads and each are actually gonna be using one of these uh boxes in this case we call this parallelism because all six threads are using their own memory and they are not actually trying to access each other's memory so it's in they are working in parallel and they basically have efficiency a hundred percent in the sense that well if we launch 50 of them and we have actually 50 elements in that array then given enough cpu power they're gonna run 50 times faster than if we didn't run them multi-threaded abstracting of course the the time it takes to create the thread and join it and what not so this is parallelism now concurrency is well remember when we had that little males variable so we had a males variable at one point which was just the number of males that were being sent in a mailbox and this guy was accessed by many many many threads right and those trades were processing the the mails themselves right so we can denote the threads by just some arrows right we have here let's say six as above and they were all accessing the same part in memory and this is what concurrencies they are all threads are concurring over the same uh shared memory right and usually you don't want this because well if all of them all they are doing are actually entering a critical section where they have to set a certain variable this is shared between all of them then that's not any more efficient than having just one thread so usually concurrency is sort of a a bad thing but of course still needed but we should actually try to optimize as much as possible uh so that we don't have to encounter it right so here this males variable was read and modified by all these threads at the same time right this is concurrency okay now let's take a look at some examples here uh we're going to take a look at some old examples that we have already seen um one of them is where we calculated the sum of all of the first 10 primes using two threads this guy was what it was doing it was basically splitting the array in half right and giving half of it to one of the threads and another half to the other right and uh because there's no clashing the they don't access the same memory and they don't uh write in the same place in memory at the same or at all that means that they are in parallel running and they don't actually have any sort of in uh mutexes so that's perfectly fine uh now this doesn't mean that uh if we have two tracks that are that are running in power we cannot have concurrency between them so there could be threats that are also parallel and concurrent at the same time uh but it's the degree of concurrency or parallelism that matters right the more parallelism you have the better the performance the less the more concurrence you have the worse the performance okay in this case basically the efficiency is a hundred percent uh if you only take a look at concurrency we have no concurrency here uh we're just getting the results and of course this uh it simply runs on two threads right so we get two sums and they are all both summed at uh in the main thread here but if we were to divide it in even more pieces like let's say eight threads then uh it would really be more efficient while obstructing again the creation of the stress and sending the information and the the time it takes to join them and as a matter of fact it would be a nice exercise for you to try and have a have an array that its sum is calculated using uh any number of trends so you just kind of input the number of tiles either here or on the actual on the keyboard and uh try to have them run that way now as for concurrency as i said this uh was one of the first uh examples that we've encountered and we have to use a mutex here we had the males variable and these males were modified every time but uh here i added a sleep of one which really means process this mail and this processing takes one second right so imagine that there's a process behind it actually takes one second so if you launch this you're gonna see that uh only one mail is processed in one second that means that all the even though we have multiple threads the performance is not any better than if we had just one simply because it's the concurrency is basically 100 everything that we're doing here is concurrent right and this is kind of not ideal that's all for this video um as i said it's the difference is actually very simple so i hope you got something out of this video if you do have any questions about it please do leave them down comment below or on our discord server again the source code for both of these uh programs can be found on our website link in the description below take care bye
Info
Channel: CodeVault
Views: 1,449
Rating: 4.9466667 out of 5
Keywords: codevault, c (programming language), parallelism, concurrency, threads
Id: 56OSQSoRR04
Channel Id: undefined
Length: 6min 29sec (389 seconds)
Published: Sun Jan 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.