Tuesday, December 22, 2009

PThread RWLock

For thread synchronization, use of mutex (mutual exclusion) is a very common. Mutex doesn't provide more advanced synchronization, when one thread is locking a mutex, another thread can't acquire it until it has been released. It's very expensive syncronization for some case.

POSIX threading library provide another high level synchronization, a RWLock (read and write lock), when locking in read mode, another thread can acquire the read lock (shared lock) but not with the write lock, with write lock, another thread can't acquire the lock until it has been unlocked (exclusive lock). This clear, with a RWLock mechanism, we can't avoid expense of mutex when we try only to read. No inconsistency effect when some thread try to read the same protected data.

The POSIX functions for RWLock operation:

Lock initialization

Lock in read mode

Lock in write mode

Release lock