Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does std::mutex prevent threads from modifying?

What part of memory gets locked by mutex when .lock() or .try_lock(), is it just the function or is it the whole program that gets locked?

like image 947
BackSpace7777777 Avatar asked Aug 01 '26 20:08

BackSpace7777777


1 Answers

Nothing is locked except the mutex. Everything else continues running (until it tries to lock an already locked mutex that is). The mutex is only there so that two threads cannot run the code between a mutex lock and a mutex unlock at the same time.

like image 183
OmnipotentEntity Avatar answered Aug 04 '26 10:08

OmnipotentEntity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!