Hello All,
I have a small doubt w.r.t using mutex locks. Say we have a thread-safe function:
int counter;
void foo()
{
LOCK_MUTEX(); // say by pthread_mutex_lock
++counter;
UNLOCK_MUTEX();
}
Now, i understand that this is thread safe because only one thread can "own" a mutex at a time and thus counter is incremented by only one thread at a time. So, if i'm right, it goes something like this:
say thread1 has the mutex, then thread2 is blocked until thread2 releases the mutex. So, when thread2 comes to the LOCK_MUTEX() call, it is blocked and the Operating System unblocks the thread when it knows that the mutex has been released.
Questions:
1) Is this understanding correct?
2) Does pthread_mutex_lock() make some kernel level calls and interact with the host OS? Could anyone please give a little more details as to how this is done?
Thanks!