All the documentation I've read on the pthreads mutex states only that a mutex prevents multiple threads from accessing shared memory, but how do you specify in the program what exactly is that? Is it all the global variables in the program, the variables being accessed between the locking and unlocking functions, or... ? Everything I've found on pthreads, including the examples, is irritatingly vague.
a mutex prevents multiple threads from accessing shared memory
The above is an incorrect statement. By itself, a mutex does not do that. It lets you build code that prevents multiple threads from accessing shared memory or other resources concurrently, but it does not lock anything by itself.
You can build a program that uses a mutex to prevent multiple threads from executing specific pieces of code concurrently. If these pieces of code happen to be accessing a shared memory region, and no other code would try accessing that region concurrently without locking the mutex, then the effect would be that of "locking" that region of memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With