EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
RwLock Class Reference

Overview

A wrapper class for the EtcPal read-write lock type.

Note: The etcpal::RwLock functions are not normally used directly - prefer usage of the RAII types etcpal::ReadGuard and etcpal::WriteGuard to manage locking and unlocking of an etcpal::RwLock.

Example usage:

class ThreadSafeCounter
{
public:
ThreadSafeCounter() = default;
// Multiple threads/readers can read the counter's value at the same time.
unsigned int get() const
{
etcpal::ReadGuard read_lock(lock_);
return value_;
}
// Only one thread/writer can increment/write the counter's value.
void increment()
{
etcpal::WriteGuard write_lock(lock_);
++value_;
}
// Only one thread/writer can reset/write the counter's value.
void reset()
{
etcpal::WriteGuard write_lock(lock_);
value_ = 0;
}
private:
mutable etcpal::RwLock lock_;
unsigned int value_ = 0;
};
int main()
{
ThreadSafeCounter counter;
auto increment_and_print = [&counter]()
{
for (int i = 0; i < 3; i++)
{
counter.increment();
std::cout << counter.get() << '\n';
}
};
etcpal::Thread thread1(increment_and_print);
etcpal::Thread thread2(increment_and_print);
thread1.Join();
thread2.Join();
std::cout << counter.get() << '\n'; // Outputs '6'
return 0;
}
Read lock guard around a read-write lock.
Definition: rwlock.h:232
A wrapper class for the EtcPal read-write lock type.
Definition: rwlock.h:112
etcpal_rwlock_t & get()
Get a reference to the underlying etcpal_rwlock_t type.
Definition: rwlock.h:211
A thread class, modeled after std::thread.
Definition: thread.h:143
Write lock guard around a read-write lock.
Definition: rwlock.h:291

See rwlock (Read-Write Locks) for more information.

Public Member Functions

 RwLock ()
 Create a new read-write lock.
 
 ~RwLock ()
 Destroy the read-write lock.
 
 RwLock (const RwLock &other)=delete
 
RwLockoperator= (const RwLock &other)=delete
 
 RwLock (RwLock &&other)=delete
 
RwLockoperator= (RwLock &&other)=delete
 
bool ReadLock ()
 Access the read-write lock for reading. More...
 
bool TryReadLock (int timeout_ms=0)
 Try to access the read-write lock for reading. More...
 
void ReadUnlock ()
 Release a read lock on the read-write lock. More...
 
bool WriteLock ()
 Access the read-write lock for writing. More...
 
bool TryWriteLock (int timeout_ms=0)
 Try to access the read-write lock for writing. More...
 
void WriteUnlock ()
 Release a write lock on the read-write lock. More...
 
etcpal_rwlock_tget ()
 Get a reference to the underlying etcpal_rwlock_t type.
 

Member Function Documentation

◆ ReadLock()

bool ReadLock ( )
inline

Access the read-write lock for reading.

Returns
The result of etcpal_rwlock_readlock() on the underlying read-write lock.

◆ ReadUnlock()

void ReadUnlock ( )
inline

Release a read lock on the read-write lock.

See etcpal_rwlock_readunlock().

◆ TryReadLock()

bool TryReadLock ( int  timeout_ms = 0)
inline

Try to access the read-write lock for reading.

Returns when either the read lock is acquired or the timeout expires. NOTE: Timeout values other than 0 or ETCPAL_WAIT_FOREVER are typically only honored on real-time platforms. See the table in rwlock (Read-Write Locks) for more information. On platforms where timeouts are not honored, passing 0 for timeout_ms executes a poll for the lock returning immediately, while any other value executes the equivalent of ReadLock().

Parameters
timeout_msHow long to wait to acquire the read lock, in milliseconds. Default is to poll and return immediately.
Returns
The result of etcpal_rwlock_timed_readlock() on the underlying read-write lock.

◆ TryWriteLock()

bool TryWriteLock ( int  timeout_ms = 0)
inline

Try to access the read-write lock for writing.

Returns when either the read lock is acquired or the timeout expires. NOTE: Timeout values other than 0 or ETCPAL_WAIT_FOREVER are typically only honored on real-time platforms. See the table in rwlock (Read-Write Locks) for more information. On platforms where timeouts are not honored, passing 0 for timeout_ms executes a poll for the lock returning immediately, while any other value executes the equivalent of WriteLock().

Parameters
timeout_msHow long to wait to acquire the write lock, in milliseconds. Default is to poll and return immediately.
Returns
The result of etcpal_rwlock_timed_writelock() on the underlying read-write lock.

◆ WriteLock()

bool WriteLock ( )
inline

Access the read-write lock for writing.

Returns
The result of etcpal_rwlock_writelock() on the underlying read-write lock.

◆ WriteUnlock()

void WriteUnlock ( )
inline

Release a write lock on the read-write lock.

See etcpal_rwlock_writeunlock().


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