PReadWriteLock Class Reference

Detailed Description

The PReadWriteLock class provides access serialization between threads.

A read-write lock is a synchronization tool for protecting resources that can be accessed for reading and writing. This type of lock is useful if you want to allow multiple threads to have simultaneous read-only access, but as soon as one thread wants to write to the resource, all other threads must be blocked until the writing is complete.

In many cases, PReadWriteLock is a direct competitor to PMutex. PReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequently.

To ensure that writers aren't blocked forever by readers, readers attempting to obtain a lock will not succeed if there is a blocked writer waiting for access, even if the lock is currently only accessed by other readers. Also, if the lock is accessed by a writer and another writer comes in, that writer will have priority over any readers that might also be waiting.

Like PMutex, a PReadWriteLock can be recursively locked by the same thread when constructed with true. In such cases, Unlock() must be called the same number of times LockForWrite() or LockForRead() was called. Note that the lock type cannot be changed when trying to lock recursively, i.e. it is not possible to lock for reading in a thread that already has locked for writing (and vice versa).

Definition at line 42 of file PReadWriteLock.h.

#include <PReadWriteLock.h>

Constructor & Destructor Documentation

◆ PReadWriteLock() [1/2]

PReadWriteLock::PReadWriteLock ( bool  isRecursive = false)

Constructs a new read-write lock.

The mutex is created in an unlocked state. If isRecursive is true, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of Unlock() calls have been made. Otherwise a thread may only lock a mutex once. The default is "non recursive".

◆ PReadWriteLock() [2/2]

PReadWriteLock::PReadWriteLock ( const PReadWriteLock other)

Constructs a shared copy of other.

◆ ~PReadWriteLock()

virtual PReadWriteLock::~PReadWriteLock ( )
virtual

Destroys this object.

Member Function Documentation

◆ LockForRead()

void PReadWriteLock::LockForRead ( )

Locks the lock for reading.

This function will block the current thread if another thread has locked for writing. It is not possible to lock for read if the thread already has locked for write.

◆ LockForWrite()

void PReadWriteLock::LockForWrite ( )

Locks the lock for writing.

This function will block the current thread if another thread (including the current) has locked for reading or writing (unless the lock has been created using the recursive mode). It is not possible to lock for write if the thread already has locked for read.

◆ operator=()

PReadWriteLock& PReadWriteLock::operator= ( const PReadWriteLock other)

Performs a shared copy of other to this object.

◆ TryLockForRead()

bool PReadWriteLock::TryLockForRead ( papillon::int32  timeOutMs)

Attempts to lock for reading.

This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked for writing, this function will wait for at most timeout milliseconds for the lock to become available.

Note: Passing a negative number as the timeout is equivalent to calling LockForRead(), i.e. this function will wait forever until lock can be locked for reading when timeout is negative.

If the lock was obtained, the lock must be unlocked with Unlock() before another thread can successfully lock it for writing.

It is not possible to lock for read if the thread already has locked for write.

◆ TryLockForWrite()

bool PReadWriteLock::TryLockForWrite ( papillon::int32  timeOutMs)

Attempts to lock for writing.

If the lock was obtained, this function returns true; otherwise, it returns false immediately.

The lock attempt will fail if another thread has locked for reading or writing.

If the lock was obtained, the lock must be unlocked with Unlock() before another thread can successfully lock it.

It is not possible to lock for write if the thread already has locked for read.

◆ Unlock()

void PReadWriteLock::Unlock ( )

Unlocks the mutex.

Attempting to unlock a mutex in a different thread to the one that locked it results in an error.