Reputation: 67
What do the pthread_mutex_lock()
and pthread_mutex_unlock()
functions really do. I understand that the lock makes it so that the code is blocked until it is unlocked again. I'm still confused on what happens in between that period where it is locked and unlocked.
Upvotes: 2
Views: 1932
Reputation: 11240
At the most basic, every hardware out there has some sort of instruction or sequence of instructions that does "test and set": "As an atomic action, test if this variable has value x, and if so, set it to y. Let me know if I am successful".
So at it's heart, we have:
mutex_var = 0
pthread_mutex_lock(&mutex_var):
while True:
if successful in atomically changing mutex_var from 0 to 1:
break
pthread_mutex_unock(&mutex_var):
mutex_var = 0
Now this code has two basic problems:
Actual implementations do a bit more. The mutex
contains more information about who currently owns the mutex, if it is locked, so that only that thread can unlock it.
So the actual code is more like:
pthread_mutex_lock():
while True:
if successful in atomically changing mutex_var from 0 to 1:
break
go to sleep waiting on this mutex
pthread_mutex_unlock():
verify that this thread owns the mutex
set mutex_var back to 0
wake up all threads waiting on this mutex
Upvotes: 0
Reputation: 43
In C, this is the basic way to lock and unlock mutexes is
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
Basically pthread_mutex_unlock() will stop the lock and wait until the program asks for another lock to happen.
Upvotes: 2