Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What part of memory does a mutex lock? (pthreads)

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.

like image 545
dreta Avatar asked Oct 03 '13 08:10

dreta


1 Answers

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.

like image 168
Sergey Kalinichenko Avatar answered Sep 23 '22 06:09

Sergey Kalinichenko