OPERATING SYSTEMS (CS F372)
Mutex
Barsha Mitra
BITS Pilani CSIS Dept., BITS Pilani, Hyderabad Campus
Hyderabad Campus
acquire()
❖ When the state is unlocked, acquire() changes the state to
locked and returns immediately
❖ When the state is locked, acquire() blocks until a call to release()
in another thread changes it to unlocked, then the acquire() call
resets it to locked and returns
BITS Pilani, Hyderabad Campus
release()
❖ The release() method should only be called in the locked state; it
changes the state to unlocked and returns immediately
❖ If an attempt is made to release an unlocked lock, may lead to
undefined behavior
BITS Pilani, Hyderabad Campus
pthread_mutex_init()
❖ int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr);
❖ Initialize the mutex referenced by mutex with attributes specified by attr
❖ If attr is NULL, the default mutex attributes are used
❖ If successful, the function returns zero; otherwise, an error number is
returned to indicate the error
❖ Upon successful initialization, the state of the mutex becomes initialized and
unlocked
BITS Pilani, Pilani Campus
pthread_mutex_destroy()
❖ int pthread_mutex_destroy(pthread_mutex_t *mutex);
❖ Destroys the mutex object referenced by mutex; the mutex object
becomes uninitialized
❖ It is safe to destroy an initialized mutex that is unlocked. Attempting to
destroy a locked mutex results in undefined behavior.
❖ If successful, the function returns zero; otherwise, an error number is
returned to indicate the error
BITS Pilani, Pilani Campus
pthread_mutex_lock()
❖ int pthread_mutex_lock(pthread_mutex_t *mutex);
❖ Mutex object referenced by mutex shall be locked
❖ If the mutex is already locked, the calling thread shall block until the
mutex becomes available
❖ If successful, the function returns zero; otherwise, an error number is
returned to indicate the error
BITS Pilani, Pilani Campus
pthread_mutex_unlock()
❖ int pthread_mutex_unlock(pthread_mutex_t *mutex);
❖ Releases the mutex object referenced by mutex
❖ If there are threads blocked on the mutex object referenced by mutex,
when pthread_mutex_unlock() is called, resulting in the mutex
becoming available, the scheduling policy shall determine which
thread shall acquire the mutex.
❖ If successful, the function returns zero; otherwise, an error number is
returned to indicate the error
BITS Pilani, Pilani Campus
Thank You
BITS Pilani, Hyderabad Campus