BRICKS
Small, useful blocks of code, to build bigger things.
Loading...
Searching...
No Matches
bricks::rw_lock< Class > Class Template Reference

A reader-writer lock. More...

#include <rw_lock.hpp>

Public Types

using value_type = Class
 
using read_guard = detail::read_guard<rw_lock>
 
using write_guard = detail::write_guard<rw_lock, std::shared_mutex>
 

Public Member Functions

auto read () const noexcept -> read_guard
 Locks the resource for reading, blocking until the lock is acquired.
 
auto write () noexcept -> write_guard
 Locks the resource for writing, blocking until the lock is acquired.
 

Friends

class detail::read_guard< rw_lock >
 
class detail::write_guard< rw_lock, std::shared_mutex >
 

Detailed Description

template<class Class>
class bricks::rw_lock< Class >

A reader-writer lock.

This class implements a reader-writer lock around a shared resource. The resource can be read by multiple threads at the same time, but only one thread can write to the resource at a time. Read access is read only, write access is read-write.

Example usage:

vec.write()->push_back(4); // Guaranteed to be thread safe write.
INFO("Vector size: ", vec.read()->size()); // Guaranteed to be thread safe read.
// Many reader locks can be held at the same time.
{
auto r1 = vec.read();
auto r2 = vec.read();
const auto safe_read = vec.read();
for (const auto& i : *safe_read) {
INFO("Vector element: ", i);
}
INFO("Vector size: ", r1->size());
INFO("First element: ", r2->front());
} // Read locks are released here.
// Only one writer lock can be held at a time.
{
auto safe_write = vec.write();
std::sort(safe_write->begin(), safe_write->end());
} // Write lock is released here.

Very similar to rust's std::sync::RwLock.

Template Parameters
ClassThe data type to be protected.

Member Typedef Documentation

◆ read_guard

template<class Class >
using bricks::rw_lock< Class >::read_guard = detail::read_guard<rw_lock>

◆ value_type

template<class Class >
using bricks::rw_lock< Class >::value_type = Class

◆ write_guard

template<class Class >
using bricks::rw_lock< Class >::write_guard = detail::write_guard<rw_lock, std::shared_mutex>

Member Function Documentation

◆ read()

template<class Class >
auto bricks::rw_lock< Class >::read ( ) const -> read_guard
inlinenoexcept

Locks the resource for reading, blocking until the lock is acquired.

Returns
read_guard An RAII style read guard, which will release the lock when it goes out of scope.

◆ write()

template<class Class >
auto bricks::rw_lock< Class >::write ( ) -> write_guard
inlinenoexcept

Locks the resource for writing, blocking until the lock is acquired.

Returns
write_guard An RAII style write guard, which will release the lock when it goes out of scope.

Friends And Related Symbol Documentation

◆ detail::read_guard< rw_lock >

template<class Class >
friend class detail::read_guard< rw_lock >
friend

◆ detail::write_guard< rw_lock, std::shared_mutex >

template<class Class >
friend class detail::write_guard< rw_lock, std::shared_mutex >
friend

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