Explain read and write locks ?

There reader and writer locks are variant of spin locks and another specialized concurrency regulation mechanism. This is suitable when critical section is used between separate threads either read from or
write to a shared data area, but don't do both. Multiple reader threads are allowed inside a critical region simultaneously, but not write. 

Reader spin locks are defined as follows:

rwlock_t myrwlock = RW_LOCK_UNLOCKED;
read_lock(&myrwlock); /* Acquire reader lock */
/* ... Critical Region ... */
read_unlock(&myrwlock); /* Release lock */

If a writer thread enters into a critical section, other reader or writer threads are not
allowed inside. To use writer spin locks, defined as follows:
 

rwlock_t myrwlock = RW_LOCK_UNLOCKED;
write_lock(&myrwlock); /* Acquire writer lock */
/* ... Critical Region ... */
write_unlock(&myrwlock); /* Release lock */

Previous Post Next Post

Contact Form