Fawkes API  Fawkes Development Version
fawkes::ReadWriteLock Class Reference

Read/write lock to allow multiple readers but only a single writer on the resource at a time. More...

#include <core/threading/read_write_lock.h>

Inheritance diagram for fawkes::ReadWriteLock:

Public Types

enum  ReadWriteLockPolicy { RWLockPolicyPreferWriter, RWLockPolicyPreferReader }
 The policy to use for the read/write lock. More...
 

Public Member Functions

 ReadWriteLock (ReadWriteLockPolicy policy=RWLockPolicyPreferWriter)
 Constructor. More...
 
virtual ~ReadWriteLock ()
 Destructor. More...
 
void lock_for_read ()
 Aquire a reader lock. More...
 
void lock_for_write ()
 Aquire a writer lock. More...
 
bool try_lock_for_read ()
 Tries to aquire a reader lock. More...
 
bool try_lock_for_write ()
 Tries to aquire a writer lock. More...
 
void unlock ()
 Release the lock. More...
 

Detailed Description

Read/write lock to allow multiple readers but only a single writer on the resource at a time.

This can be used if you have a value that only a few writers modify but several readers use. In this case the readers can read all at the same time as long as there is no writer modifying the value.

See also
example_rwlock.cpp
Author
Tim Niemueller

Definition at line 32 of file read_write_lock.h.

Member Enumeration Documentation

◆ ReadWriteLockPolicy

The policy to use for the read/write lock.

Enumerator
RWLockPolicyPreferWriter 

Prefer writers over readers.

A writer is granted prefered access to the lock. This means that the writer can aquire the lock as soon as all readers unlocked it not matter if there are readers queued and waiting for the lock. This is the default behaviour. Not with multiple writers you can run into the problem of reader starvation.

RWLockPolicyPreferReader 

Prefer readers over writers.

This is similar to the writer preference. Readers will be allowed to aquire the lock no matter if there is a writer enqueued for the lock. Not that with many readers (depending on the time they aquire the lock this can already start with two or three readers) you can run into the problem of writer starvation: the writer can never aquire the lock.

Definition at line 38 of file read_write_lock.h.

Constructor & Destructor Documentation

◆ ReadWriteLock()

fawkes::ReadWriteLock::ReadWriteLock ( ReadWriteLockPolicy  policy = RWLockPolicyPreferWriter)

Constructor.

Parameters
policyThe read/write lock policy to use. The default is to prefer writers.

Definition at line 60 of file read_write_lock.cpp.

◆ ~ReadWriteLock()

fawkes::ReadWriteLock::~ReadWriteLock ( )
virtual

Destructor.

Definition at line 85 of file read_write_lock.cpp.

Member Function Documentation

◆ lock_for_read()

void fawkes::ReadWriteLock::lock_for_read ( )

Aquire a reader lock.

This will aquire the lock for reading. Multiple readers can aquire the lock at the same time. But never when a writer has the lock. This method will block until the lock has been aquired.

Definition at line 98 of file read_write_lock.cpp.

Referenced by PclViewerTransferThread::lock_for_read(), fawkes::ScopedRWLock::relock(), and fawkes::ScopedRWLock::ScopedRWLock().

◆ lock_for_write()

void fawkes::ReadWriteLock::lock_for_write ( )

Aquire a writer lock.

This will aquire the lock for writing. Only a single writer at a time will be allowed to aquire the lock. This method will block until the lock has been aquired.

Definition at line 110 of file read_write_lock.cpp.

Referenced by PclViewerTransferThread::loop(), fawkes::ScopedRWLock::relock(), and fawkes::ScopedRWLock::ScopedRWLock().

◆ try_lock_for_read()

bool fawkes::ReadWriteLock::try_lock_for_read ( )

Tries to aquire a reader lock.

This will try to aquire the lock for reading. This will succeed if no writer has aquired the lock already. Multiple readers may aquire the lock.

Returns
true, if the lock could be aquired, false otherwise.

Definition at line 123 of file read_write_lock.cpp.

◆ try_lock_for_write()

bool fawkes::ReadWriteLock::try_lock_for_write ( )

Tries to aquire a writer lock.

This will try to aquire the lock for writing. This will succeed if the read/write lock is currently unlocked. No other threads may hold this lock at the same time. Neither for writing nor for reading.

Returns
true, if the lock has been aquired, false otherwise.

Definition at line 136 of file read_write_lock.cpp.

◆ unlock()

void fawkes::ReadWriteLock::unlock ( )

Release the lock.

Releases the lock, no matter whether it was locked for reading or writing.

Definition at line 146 of file read_write_lock.cpp.

Referenced by PclViewerTransferThread::loop(), PclViewerTransferThread::unlock(), fawkes::ScopedRWLock::unlock(), and fawkes::ScopedRWLock::~ScopedRWLock().


The documentation for this class was generated from the following files: