std::mutex and preventing data races in C++ | Introduction to Concurrency in Cpp

  Рет қаралды 11,861

Mike Shah

Mike Shah

Күн бұрын

Пікірлер: 47
@kaan8964
@kaan8964 Жыл бұрын
Clear explanation, good lesson structure, and very informative. Your lessons are great, thank you
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@damondouglas
@damondouglas 5 ай бұрын
Thank you so much for this series. I really like how your examples build off each other so its easy to introduce the new concept. As you said, it's hard for our brains to compute concurrently
@MikeShah
@MikeShah 5 ай бұрын
Cheers -- thank you Damon!
@forbiddenc9202
@forbiddenc9202 7 ай бұрын
Been a great lecture , also learning your c++ series, Great series
@MikeShah
@MikeShah 7 ай бұрын
Cheers!
@aarfeenanees9147
@aarfeenanees9147 Жыл бұрын
Thank you so much. This improved (and corrected) my understanding of mutex drastically. If i may make a request, I'd like you to illustrate difference between data race and race conditions using c++. There's no video content on the internet explaining these two terms via c++. I have read a few answers on various forums, but they weren't really helpful as they ranged from claiming these concepts are the same to others claiming they are totally unrelated terms.
@MikeShah
@MikeShah Жыл бұрын
Noted -- will add this to the wishlist for concurrency series
@SupreetSinghsuppi
@SupreetSinghsuppi Жыл бұрын
Thanks Mike. I have been studying this. This helps.
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@sulabhhoon
@sulabhhoon 12 күн бұрын
good video , but mutex and binary semaphore are same ?
@MikeShah
@MikeShah 12 күн бұрын
I should have been more specific -- they are similar, and you can use a binary semaphore in a 'mutex-like fashion to guard a resource' but the mechanism of a 'lock' (mutex) and a signal (a binary semaphore that increments/decrements) are indeed different :)
@JasonLee-pr4sx
@JasonLee-pr4sx 3 ай бұрын
in function shared_value_increment, you can add a layer of loop to increase the execution time. On my Laptop, it will always shows 100 if there is only one line to execute.
@MikeShah
@MikeShah 3 ай бұрын
May get lucky, but a lock is needed to ensure operating system interrupt and context switch does not interfere and you get a data race 🙂
@carminex
@carminex Жыл бұрын
Great video, but please, fix your sound
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@SaschaRobitzki
@SaschaRobitzki Жыл бұрын
Unfortunately, I haven't seen the fluctuations demonstrated so far on my local machine. I tried up to 100,000 threads and the result is always constant and as expected.
@MikeShah
@MikeShah Жыл бұрын
Are you running with optimizations by chance? That's quite lucky that you are getting a consistent value :) In reality, machines won't spawn 100,000 threads either, so perhaps there is a resource limit to how many threads can run as well that's getting more predictive behavior. That's what makes these bugs tricky however!
@SaschaRobitzki
@SaschaRobitzki Жыл бұрын
​@@MikeShah No optimization. 100 threads, -> 7,100 microseconds 1,000 threads -> 85,000 10,000 threads -> 1,200,000 100,000 threads -> 33,300,000 The static int gets incremented, I don't have the feeling I hit a thread limit. Otherwise, why would it get incremented to 100,000 with 100,000 threads? mutex or not doesn't affect the result.
@MikeShah
@MikeShah Жыл бұрын
@@SaschaRobitzki Interesting -- only thing I can think is the compiler getting rid of the threads and inlining everything. Very strange that this works without a mutex. Perhaps trying two different share variables that are accumulating and add some more work in the 'shared_value_increment' function.
@SaschaRobitzki
@SaschaRobitzki Жыл бұрын
@@MikeShah I tried GCC's -fno-inline-small-functions and -fno-inline with no effect.
@SaschaRobitzki
@SaschaRobitzki Жыл бұрын
@@MikeShah I switched to Microsoft's compiler and used a vanilla project in VS 2022. I got the same result as before. I added more work to shared_value_increment(), but that didn't make any difference. I guess there must be something wrong with my code, though it looks exactly like on your screen here.
@master138
@master138 Жыл бұрын
Thank you for the video. Why did you declare the std::mutex globally? And not inside the function? Can we reuse that mutex class anywhere?
@MikeShah
@MikeShah Жыл бұрын
std::mutex was made global because every thread needs to be able to access (i.e. check if the mutex is available).
@PDL_AlexBibou
@PDL_AlexBibou Жыл бұрын
Great video, how would i do if i want to increment a value that is not global ?
@PDL_AlexBibou
@PDL_AlexBibou Жыл бұрын
a value that i would pass to the function and that i wanna be able to retrieve in the main later
@MikeShah
@MikeShah Жыл бұрын
@@PDL_AlexBibou You can still pass a value by reference for instance :)
@PDL_AlexBibou
@PDL_AlexBibou Жыл бұрын
@@MikeShah ah i see thanks for the answer man
@svssukesh1170
@svssukesh1170 Жыл бұрын
Hi, Thanks and I really liked the way you explained this concept...But I have a small doubt and hope some one can answer it. Does the mutex lock actually locking the other threads to access the shared_value or is it locking the other threads from executing the instructions which are inside the mutex block "shared_value = shared + 1". My doubt is, lets say if one thread is exciting the function shared_value_increment(), which has the mutex_lock...lets say I have a another fuction which just returns the value of shred_value as void return_shared_value(){ return shared_value; } lets say if I am calling this return_shared_value in another thread, will the first thread which has the muter_lock in shared_value_increment() block the second thread which is just trying to read the value or does the mutex lock only lock the other threads which are trying to increment the value using shared_value_increment()
@MikeShah
@MikeShah Жыл бұрын
Mutex is effectively creating a 'critical section' so no other thread can access that code (i.e. enter the critical region). If you want to protect the actual shared_value you can use an std::atomic (covered in another video on this series here: kzbin.info/www/bejne/nJCmZZiPraeKmrM )
@pm1783
@pm1783 2 жыл бұрын
Mutexes and Binary Semaphores are definitely not the same thing, although they are similar.
@MikeShah
@MikeShah 2 жыл бұрын
True, mutex, binary semaphore, and counting semaphores are different.
@snehalsha4051
@snehalsha4051 2 жыл бұрын
The main difference between a binary semaphore and a mutex is that a binary semaphore can be released by a task other than the task that acquired it, while a mutex can only be released by the task that acquired it.
@wika96
@wika96 2 жыл бұрын
Don't we have to initialize mutex lock
@MikeShah
@MikeShah 2 жыл бұрын
If you're using something like pthread's library, then you do initialize the lock. No need to here. And in reality (see the next video in playlist), we probably want to wrap the mutex and use some sort of 'scoped guard' :)
@wika96
@wika96 2 жыл бұрын
@@MikeShah Your videos are awesome.
@MikeShah
@MikeShah 2 жыл бұрын
@@wika96 Cheers, thank you for the kind words.
@gregwoolley
@gregwoolley Жыл бұрын
You may not be aware, your audio is choppy. The beginning of some of your words is chopped off, making it difficult to understand those words.
@MikeShah
@MikeShah Жыл бұрын
Should be fixed on all newer videos 🙂
@gregwoolley
@gregwoolley Жыл бұрын
@@MikeShahCool, glad to hear that because the content of your videos is great, it will be good to hear everything you say. Thank you for your good work!
@MikeShah
@MikeShah Жыл бұрын
Cheers! @@gregwoolley
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
C++ Weekly - Ep 404 - How (and Why) To Write Code That Avoids std::move
8:50
C++ Weekly With Jason Turner
Рет қаралды 35 М.
why do header files even exist?
10:53
Low Level
Рет қаралды 453 М.
Recursive Mutex In C++
20:46
CppNuts
Рет қаралды 26 М.
Return Value Optimization and Copy Elision in C++
9:54
mCoding
Рет қаралды 40 М.
Using a trylock | Introduction to Concurrency in C++
18:30
Mike Shah
Рет қаралды 4,2 М.
What is Mutex in C++ (Multithreading for Beginners)
12:29
CodeBeauty
Рет қаралды 49 М.
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 405 М.
Monster My Best Friend 🥹❤️👻 #shorts Tiktok
1:01
BETER BÖCÜK
Рет қаралды 29 МЛН
Pixel 7 и 7 Pro с Face ID - лучше iPhone 14 Pro!
21:12
Rozetked
Рет қаралды 457 М.
СИЖУ БЕЗ ЕДЫ, ПЬЮ ОДНУ ВОДИЧКУ.
21:37
Быть Добру
Рет қаралды 79 М.
Do YOU Understand WHAT JUST HAPPENED!? 😂 #shorts
0:57
LankyBox World
Рет қаралды 1,6 МЛН
ТЕЛЕФОН МЕНЯЕТ ЦВЕТ😅 #upx
0:34
RanF
Рет қаралды 639 М.
ЛАЙФХАК НА КУХНЕ ! 🧐🤦🏻‍♂️ #shorts #лайфхак
0:15
Крус Костилио
Рет қаралды 109 М.