Friday, November 30, 2012

mutex Vs read-write lock

what is the diff between mutex and readwrite lock?

Mutex:

A Mutex is an OS object which supports a state of ‘locked’ or ‘available’. What it means is that if multiple threads attempt to lock the Mutex, only one will ‘own’ that lock while others will be waiting in the queue behind each other.

The waiting threads will block the function call from where they are initiated until Mutext is unlocked ‘available’ and a thread gets chance to claim it.

There are two types of Mutex, ‘named’ and ‘unnamed’. A named Mutext can be accessed from different processes running on the system whereas unnamed Mutex can only be used from inside a single process. On Windows OS, the Mutex is replaced with Critical Section which is faster, unnamed and can be used from a single process only.

Read-Write Lock:

It is a file system construct that works over peer-to-peer file sharing or any other file sharing schemes.
They are similar to Mutex with the difference that once the file is locked, other threads are denied the lock request and are not being put in the waiting queue.

File locking supports ‘shared-read’ request. What it means is threads requesting for reading the file, get the lock and can read the file however there can be only one file write access.