Give a man a fish and he'll eat for a day. Teach a man to fish and he'll eat every day. You are outstanding teacher mate. Your approach to keep these amazing videos free of cost makes you great!!! Amazing!!!!
The operations performed by a thread after acquiring a lock are called "critical section". This helps in achieving thread synchronisation. Thanks for the video!
I want you to know you have taught me more about the C language in 10 minutes than an entire university lecture did in 80 minutes. I need to learn this for a project and you have made my life so much easier. Thank you so much.
Can’t thank you enough for your detailed and succinct explanations! When I began diving in to multithreaded programming with C++17 I became overwhelmed pretty quickly
Subscribed to your channel man. Its crazy how you break down everything into making them seem so easy! Much appreciated dude and I really hope more see your channel, much love!
I purely watched this playlist to understand what the heck my OS prof was teaching when she taught us semaphores (although for processes) and I couldn't understand at that time. Loved it !
Thank you very much! You explained it so well and clearly; when my university lecturer explained it to me, I did not understand anything. I really like your channel.
Great video, explained how lock and unlock work really well. I was having trouble understanding what it was with my professors explanation lol. Thanks for explaining it, you definitely deserve more views and subs!
Race conditions can also occur on single core processors like if we consider concurrent CPU and the operations are read, increment and write. So lets say P1 starts first then P1 reads increments and then it is preempted by another process P2, then P2 does read, increment and then it is preempted by P1. Now P1 writes the value and exits after which P2 comes and writes its value thus there is a race condition
Just a slight correction at the end: You can have race conditions on single core processors. We can fake "parallel execution" by time slicing. If the two threads are sharing memory resources, then we can absolutely have a race condition during a context switch.
I love your videos. Your materials are helping me a lot more than my lecturers do and also you making it simple and more understable. Looking forward to POSIX/ SYS V semaphores. Also I would like to ask will there be coming more materials about pthreads ? ( maybe private data of threads). Best of regards code vault. Keep it up!
Thank you! And yes, the last lessons in the course are not yet decided so will add most of the suggestion from the community there (like this one ;) ).
just a note mutuale exclusion problem can heppen even for single core processors , because when the quantum ends of a processoes , another process can modifie the variable
Great tutorial. I have one comment about the likelihood of race condition happening for single-core processors. Even with a single core and a single processor but multiple threads (as it is the case for the FreeRTOS OS in embedded systems), race conditions will happen more often than not without a mutex, atomic instructions, etc.
Thanks! Interesting. I wonder now about single-core single-thread environments. Technically there still should be some race conditions but they might be encountered less often. Would you like to test that?
@@CodeVault I think the only possibility for race conditions to happen for single-core single-threaded app, is when a hardware interrupt occurs and ISR gets executed. If hardware interrupts were not present, I'm not sure race conditions can ever occur for a single-core, single-threaded app
Thank you for your great video bro. You save my life !!! Just curious a little bit from the last video. Base on my understanding, Race condition happen when 2 thread read memory at the same time. Is that possible if mutex lock data in exactly same time and race condition happen. If not why?
First things first: a race condition happens only in two cases: 1) You have one thread writing a piece of data while other threads are reading it 2) You have multiple threads writing the same piece of data Two threads reading the same piece of data won't cause a race condition. Although with mutexes a read and write could happen at the same time, they are designed so that each read and write on them is atomic, meaning there's no way other threads could read the data while another is in the process of writing it (only before it wrote to it or after that)
4 роки тому+1
Could you make a video about semaphores and pointing the differences between it and mutexes' use cases?
Well, init is surely needed when using a mutex. Although, probably, since the mutex is global in this video it gets its members automatically initialized to 0 and I think that coincides with what pthread_mutex_init sets the values to. If you try to use the a local mutex I think you'd have issues without the init function call. The pthread_mutex_destroy is not 100% necessary on some architectures but it's good practice
Bro small problem with your code.I took this as a refresher to threads.Locking and unlocking the mutex in each iteration of the loop introduces unnecessary overhead and reduces the potential benefits of multithreading. A better way is to lock and unlock it outside the loop.Anyway thanks for the video bro
Of course. But this was a short explanation and I wanted to show that the mutexes work even if multiple loops are executing at the same time. In a production environment, this would be very inefficient indeed
Maybe a minor misstatement when you say race conditions only happen on multicore systems. I think race conditions can also happen on one core processors. You could save your mails in thread 1 context switch to thread 2 and finish loop then go back to 1 and finish, then your result would only be 1,000,000 on write back. Great content though. Love this playlist.
Yes, it's definitely wrong. Race conditions can happen on any system. It's just much much rare to happen on single-core processors as the context switch would have to happen right between an assignment (which, from what I recall, is a very low chance). Maybe I will make a video on this topic to investigate what is the chance of that happening and whatnot
say in the routine() function I had 3 different operations, op1, op2, op3. I place a mutex_lock before op1, mutex_unlock after op1. Assume 3 threads. Will the other threads execute op2, and op3 when op1 is under lock ?
No, they won't. All threads will wait at the mutex_lock instruction until the lock is unlocked. Though, this still allows for race conditions on op2 and op3
They are basically the same. The first one could technically be faster since it only assigns some values but can only be used. Also, it guarantees that the lock is always initialized
It's just some attributes that you can set for the thread to run in a specific way. You can read more on this here: linux.die.net/man/3/pthread_attr_init
i have encounter an error regarding pthread_mutex_destroy(&mutex, NULL); but they are not receiving the int type for this function it just takes the refernce that's it. Hope u understand what i am trying to convey.
Do I need mutex if one thread is changing the variable, and the other only reads it ? Race condition will not occur, but there can be a collision in access. There are different opinions about the need for mutex. What's yours ?
i've got a question , sadly i am using windows and the codeblocks compiler , i tried to run without the mutex , and the result was always as expected ( n * mails , n being the number of threads)
You could do that (and I suggest you actually try + print out which thread incremented the value). But what you'll notice is that if you put the mutex lock/unlock outside the for loop, the thread that locks the mutex will make ALL the add operation and only after that will a different be able to make any add operation on that variable. So, while it's possible. It's not what you're looking for usually.
Hi, great video! I wanted to ask if we should always use mutex when using global variables? For example, I've got global arrays A, B and C. Arrays A and B are used in threads to read from them only, whereas I write (only write, don't read) to array C in threads. Can I use mutex only for array C as it's the only address that threads actually write to? Thanks in advance :)
Mutexes are only required if you have threads that write to those parts of memory. So, you're correct, you don't need a mutex for arrays A and B. That's one reason multi-threaded applications can scale well.
@@CodeVault Thanks for answer! Btw, thanks to you I was able to do my university exercise. Really appreciate clarity and easy examples in your wideos, keep it up :D
This man deserves so many more views and subscribers. So well explained. My professors need to learn from him on how to teach.
yup
i reply for the referencement
Exactly
Give a man a fish and he'll eat for a day. Teach a man to fish and he'll eat every day.
You are outstanding teacher mate. Your approach to keep these amazing videos free of cost makes you great!!! Amazing!!!!
This man is practically the best teacher I've ever had. Thank you SOOO MUCH.
The operations performed by a thread after acquiring a lock are called "critical section". This helps in achieving thread synchronisation. Thanks for the video!
I want you to know you have taught me more about the C language in 10 minutes than an entire university lecture did in 80 minutes. I need to learn this for a project and you have made my life so much easier. Thank you so much.
This is something random but it's so attractive seeing someone being so smart
Thanks haha
Can’t thank you enough for your detailed and succinct explanations! When I began diving in to multithreaded programming with C++17 I became overwhelmed pretty quickly
Man, you are an absolute legend! Thank you so much for uploading this.
you should train professors on how to teach
Thanks for explaining, it was very easy to understand.
Subscribed to your channel man. Its crazy how you break down everything into making them seem so easy! Much appreciated dude and I really hope more see your channel, much love!
Man, you really make C look very simple and lovable.
I bet you are not even close to understand how good you've done for humanity since you started this channel. Thank you very much.
I wish you were one of my professors. Every time I watch a video from you I get excited to code again.
BRO your tutorials bring me through my whole semester
This man is practically the best teacher! Sub!
dude you’re the best. Thank you so much for making these videos
The lock variable + if block example is the best example I have even seen for this Mutex topic
Thanks for making these amazing tutorials. You earned a sub
I purely watched this playlist to understand what the heck my OS prof was teaching when she taught us semaphores (although for processes) and I couldn't understand at that time. Loved it !
You're an AMAZING teacher, Sir. Thanks for this. Subscribed!
You can tell how helpful his videos are because I have not seen 1 dislike yet. You make this so understandable
Hello. This is one of the greatest tutorials I've seen. I will be watching the whole playlist, thank you for your hard work and skill in education.
Thank you very much! You explained it so well and clearly; when my university lecturer explained it to me, I did not understand anything. I really like your channel.
You saved alot of work and misunderstanding with the threads videos. Thanks alot
thank you really , you are better than almost all the teachers in this world...
all the respect
Great video, explained how lock and unlock work really well. I was having trouble understanding what it was with my professors explanation lol. Thanks for explaining it, you definitely deserve more views and subs!
Your classes are amazing! All of them! Just one thing, I think that when you say race condition, the correct term is data race. Cheers!
Man you are a God, thanks to sharing this with us!
You make complex things more easier, great job !! thank you.
Race conditions can also occur on single core processors like if we consider concurrent CPU and the operations are read, increment and write. So lets say P1 starts first then P1 reads increments and then it is preempted by another process P2, then P2 does read, increment and then it is preempted by P1. Now P1 writes the value and exits after which P2 comes and writes its value thus there is a race condition
You're right, I didn't want to go into much detail regarding this and gave a simple (albeit wrong) explanation
What a clear and simple explanation. Thanks my dude
Just a slight correction at the end: You can have race conditions on single core processors. We can fake "parallel execution" by time slicing. If the two threads are sharing memory resources, then we can absolutely have a race condition during a context switch.
That is correct.
PERFECT! looking forward to see a video about spinlock!
This man is amazing! I wish he had merch, I would love to rock a code vault T-Shirt or Hoody
Haha, thanks. I will think about launching some merch if enough people want them
Great job. Explanations are easy and clear, very useful information. Thanks a lot!!!
Thank you sir, these tutorials have made my synchronisation concepts very clear.
Thank you for the practical Mutual Exclusion video.
Mutual Exception.
You explain way more clear than my professor Park
I love your videos. Your materials are helping me a lot more than my lecturers do and also you making it simple and more understable. Looking forward to POSIX/ SYS V semaphores. Also I would like to ask will there be coming more materials about pthreads ? ( maybe private data of threads). Best of regards code vault. Keep it up!
Thank you! And yes, the last lessons in the course are not yet decided so will add most of the suggestion from the community there (like this one ;) ).
you are a gem! Thank you for the videos.
Best course on Multithreading!!
just a note mutuale exclusion problem can heppen even for single core processors , because when the quantum ends of a processoes , another process can modifie the variable
many thanks for this series, sir! you'v saved me !!!!
God send savior, HE's the ONE
Once again, UA-cam saves my homework. Thank you!
Thank you so much for the explanation on Pthread mutex!
I really enjoy your videos, thank you
Could you make a video about Condition Variables with threads?
Will do
Really a wonderful explanation. Thank you!
This is beyond helpful! Thank you so much :)
Really useful videos, just as im learning this at school :D
race cond can also happen in single core if interrupts are enabledand a variable is modified.
Its incredible, I'm French but I understood perfectly this video, thanks you
Great tutorial.
I have one comment about the likelihood of race condition happening for single-core processors. Even with a single core and a single processor but multiple threads (as it is the case for the FreeRTOS OS in embedded systems), race conditions will happen more often than not without a mutex, atomic instructions, etc.
Thanks! Interesting. I wonder now about single-core single-thread environments. Technically there still should be some race conditions but they might be encountered less often. Would you like to test that?
@@CodeVault
I think the only possibility for race conditions to happen for single-core single-threaded app, is when a hardware interrupt occurs and ISR gets executed.
If hardware interrupts were not present, I'm not sure race conditions can ever occur for a single-core, single-threaded app
arrayyy u r great ji😍
Foarte bine explicat, mersi :)
You are a gem 🙏🏻
Thank you sir , I appreciate your help !
Thank you so much for the video!
AMAZING TEACHER!!!!!!!!!!!!!!!!!! THANK YOU!!!
Thanks for the great explanation!
Thank you, you're so helpful!
Ty for the awesome videos!
Fantastic explanation!!! Thanks
thank you for this playlist
Very useful video! Thank you!
Thanks you so much you teaching method are soooo good.
Thank you for your great video bro. You save my life !!!
Just curious a little bit from the last video. Base on my understanding, Race condition happen when 2 thread read memory at the same time. Is that possible if mutex lock data in exactly same time and race condition happen. If not why?
First things first: a race condition happens only in two cases:
1) You have one thread writing a piece of data while other threads are reading it
2) You have multiple threads writing the same piece of data
Two threads reading the same piece of data won't cause a race condition.
Although with mutexes a read and write could happen at the same time, they are designed so that each read and write on them is atomic, meaning there's no way other threads could read the data while another is in the process of writing it (only before it wrote to it or after that)
Could you make a video about semaphores and pointing the differences between it and mutexes' use cases?
Yep, semaphores and barriers are also planned for this course
i comment for the referencement, you're a good guy
Even if I comment mutex_init and mutex_destroy, i can able to see mutex lock/unlock is working. Then what is the use of init/destroy function???
Well, init is surely needed when using a mutex. Although, probably, since the mutex is global in this video it gets its members automatically initialized to 0 and I think that coincides with what pthread_mutex_init sets the values to. If you try to use the a local mutex I think you'd have issues without the init function call. The pthread_mutex_destroy is not 100% necessary on some architectures but it's good practice
@@CodeVault Thank you for your clarity.
2:09 what if the threads met a race condition at reading lock?
locks are thread-safe at the OS level
great video! thanks!!!! very good job! greetings from germany
man, i wanna be your thread. such a good explanation
Bro small problem with your code.I took this as a refresher to threads.Locking and unlocking the mutex in each iteration of the loop introduces unnecessary overhead and reduces the potential benefits of multithreading. A better way is to lock and unlock it outside the loop.Anyway thanks for the video bro
Of course. But this was a short explanation and I wanted to show that the mutexes work even if multiple loops are executing at the same time. In a production environment, this would be very inefficient indeed
amazing explanation.
great explanation thank you!
Maybe a minor misstatement when you say race conditions only happen on multicore systems. I think race conditions can also happen on one core processors. You could save your mails in thread 1 context switch to thread 2 and finish loop then go back to 1 and finish, then your result would only be 1,000,000 on write back. Great content though. Love this playlist.
Yes, it's definitely wrong. Race conditions can happen on any system. It's just much much rare to happen on single-core processors as the context switch would have to happen right between an assignment (which, from what I recall, is a very low chance). Maybe I will make a video on this topic to investigate what is the chance of that happening and whatnot
@@CodeVault that would be awesome 👍🏻
say in the routine() function I had 3 different operations, op1, op2, op3. I place a mutex_lock before op1, mutex_unlock after op1. Assume 3 threads. Will the other threads execute op2, and op3 when op1 is under lock ?
No, they won't. All threads will wait at the mutex_lock instruction until the lock is unlocked. Though, this still allows for race conditions on op2 and op3
Bravo ! thanks for explaining
my question is : when we may need to create 2 threads with same function?
Whenever you want to split CPU intensive work between the threads, that's when the workload is exactly the same except for some indices.
Thanks for the wonderful video. I have a question. What happens if thread 1 throws an error before the unlock? Will thread 2 wait forever?
The lock is released, so thread 2 won't wait forever
Thank you SOOO MUCH.
Good stuff my friend
Apparently there's another way of initializing a pthread_mutex:
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
I wonder what the difference is
They are basically the same. The first one could technically be faster since it only assigns some values but can only be used. Also, it guarantees that the lock is always initialized
Race codnitions can very well occur in single core processors too. Think of pre-emption.
Exactly.
What is use case of second parameter of NULL when I create a thread. What are the use case i can change from NULL to some other?
It's just some attributes that you can set for the thread to run in a specific way. You can read more on this here: linux.die.net/man/3/pthread_attr_init
Your a gem!
you are a god
Real time example super explanation.
i have encounter an error regarding pthread_mutex_destroy(&mutex, NULL);
but they are not receiving the int type for this function it just takes the refernce that's it. Hope u understand what i am trying to convey.
The pthread_mutex_destroy only has one parameter, the mutex you are trying to destroy
Do I need mutex if one thread is changing the variable, and the other only reads it ? Race condition will not occur, but there can be a collision in access. There are different opinions about the need for mutex. What's yours ?
You will still get a race condition. I think that's called a dirty read. It depends on the situation if you need a mutex
Can we say that mutex locking forces the multithreading routine into serial?
Yes. Basically the critical section that is surrounded by a mutex lock/unlock will always be serially executed
Thank you.
Your video is the best. thx
i've got a question , sadly i am using windows and the codeblocks compiler , i tried to run without the mutex , and the result was always as expected ( n * mails , n being the number of threads)
Try incrementing more than once per thread
Either way, to get access to the pthread API you need to run it on WSL or a Linux distro.
@@CodeVault thanks for replying , i got it
Is having the mutex locks and unlock inside the for-loop the only solution? Could the mutex's have been on the outside of the for-loop?
You could do that (and I suggest you actually try + print out which thread incremented the value). But what you'll notice is that if you put the mutex lock/unlock outside the for loop, the thread that locks the mutex will make ALL the add operation and only after that will a different be able to make any add operation on that variable.
So, while it's possible. It's not what you're looking for usually.
Greate video that makes me want to migrate from windows to linux😂
For development it's definitely a must. Either to Linux or Mac... Windows is not great for development
Hi, great video! I wanted to ask if we should always use mutex when using global variables? For example, I've got global arrays A, B and C. Arrays A and B are used in threads to read from them only, whereas I write (only write, don't read) to array C in threads. Can I use mutex only for array C as it's the only address that threads actually write to? Thanks in advance :)
Mutexes are only required if you have threads that write to those parts of memory. So, you're correct, you don't need a mutex for arrays A and B. That's one reason multi-threaded applications can scale well.
@@CodeVault Thanks for answer! Btw, thanks to you I was able to do my university exercise. Really appreciate clarity and easy examples in your wideos, keep it up :D
good explanation!
Love your Videos
very underrated