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

A mutual exclusion primitive that can be used to protect shared data. More...

#include <mutex.hpp>

Public Types

using value_type = Class
 
using lock_guard = detail::write_guard<mutex, std::mutex>
 
using const_lock_guard = detail::write_guard<const mutex, std::mutex>
 

Public Member Functions

auto lock () noexcept -> lock_guard
 Locks the mutex, blocking until the lock is acquired.
 
auto lock () const noexcept -> const_lock_guard
 

Friends

class detail::write_guard< mutex, std::mutex >
 
class detail::write_guard< const mutex, std::mutex >
 

Detailed Description

template<typename Class, typename std::enable_if_t< std::is_class_v< Class >, bool > = true>
class bricks::mutex< Class, >

A mutual exclusion primitive that can be used to protect shared data.

This mutex will block threads waiting for the lock to become available. The mutex can be created by passing the data to be protected to the constructor. The data can be accessed by calling the lock() method, which returns a RAII style lock guard. The lock guard can be used to access the protected data.

Example usage:

vec.lock()->push_back(4); // Guaranteed to be thread safe write.
INFO("Vector size: ", vec.lock()->size()); // Guaranteed to be thread safe read.
{
auto lock_guard = vec.lock();
std::sort(lock_guard->begin(), lock_guard->end());
} // Lock is released here.

Member Typedef Documentation

◆ const_lock_guard

template<typename Class , typename std::enable_if_t< std::is_class_v< Class >, bool > = true>
using bricks::mutex< Class, >::const_lock_guard = detail::write_guard<const mutex, std::mutex>

◆ lock_guard

template<typename Class , typename std::enable_if_t< std::is_class_v< Class >, bool > = true>
using bricks::mutex< Class, >::lock_guard = detail::write_guard<mutex, std::mutex>

◆ value_type

template<typename Class , typename std::enable_if_t< std::is_class_v< Class >, bool > = true>
using bricks::mutex< Class, >::value_type = Class

Member Function Documentation

◆ lock() [1/2]

template<typename Class , typename std::enable_if_t< std::is_class_v< Class >, bool > = true>
auto bricks::mutex< Class, >::lock ( ) const -> const_lock_guard
inlinenoexcept

◆ lock() [2/2]

template<typename Class , typename std::enable_if_t< std::is_class_v< Class >, bool > = true>
auto bricks::mutex< Class, >::lock ( ) -> lock_guard
inlinenoexcept

Locks the mutex, blocking until the lock is acquired.

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

Friends And Related Symbol Documentation

◆ detail::write_guard< const mutex, std::mutex >

template<typename Class , typename std::enable_if_t< std::is_class_v< Class >, bool > = true>
friend class detail::write_guard< const mutex, std::mutex >
friend

◆ detail::write_guard< mutex, std::mutex >

template<typename Class , typename std::enable_if_t< std::is_class_v< Class >, bool > = true>
friend class detail::write_guard< mutex, std::mutex >
friend

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