Mutexed 0.0.1
A header-only mutex-protected value wrapper for C++20.
Loading...
Searching...
No Matches
Functions
The waiting feature

The waiting feature is enabled if H is has_cv. More...

Functions

template<typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
void llh::mutexed::Mutexed< T, M, H >::wait (Predicate &&p) const
 Waits until this is notified and the provided predicate returns true.
 
template<class Rep , class Period , typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
bool llh::mutexed::Mutexed< T, M, H >::wait_for (std::chrono::duration< Rep, Period > const &rel_time, Predicate &&p) const
 Waits until this is notified and the provided predicate returns true or until the specified duration has been spent waiting.
 
template<class Clock , class Duration , typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
bool llh::mutexed::Mutexed< T, M, H >::wait_until (std::chrono::time_point< Clock, Duration > const &timeout_time, Predicate &&p) const
 Waits until this is notified and the provided predicate returns true or until the specified time point has been passed.
 

Detailed Description

The waiting feature is enabled if H is has_cv.

It makes available the three waiting functions that mirror the three waiting methods of std::condition_variable and use them internally with a possibly_shared_lock as first argument.

Internally, the Mutexed will hold a condition-variable that will be notified with notify_all() after the unlocking that occurs whenever the inner value has been write-accessed, which happens at the end of the calls to the non-const versions of locked() and with_locked().

Here is an example of waiting on a Mutexed :

struct future_int : std::optional<int> {
void compute() { emplace(3); }
};
int main() {
Mutexed<future_int, std::mutex, has_cv> init_after;
// launching the thread that checks the result
std::thread async_after_compute([&](){
init_after.wait([](future_int const& fi){ return fi.has_value(); });
std::cout << "The result is " << init_after.get_copy().value() << std::endl;
});
// making sure it stopped at the point where it waits
std::this_thread::sleep_for(std::chrono::milliseconds(20));
// launching the thread that computes
std::thread async_compute([&](){
// change and notify
init_after.with_locked(&future_int::compute);
});
async_after_compute.join();
async_compute.join();
}

Function Documentation

◆ wait()

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
void llh::mutexed::Mutexed< T, M, H >::wait ( Predicate &&  p) const
inline

Waits until this is notified and the provided predicate returns true.

Calling this function is blocking.

The predicate is called with the inner value shared-locked if it is shared_lockable , and unique-locked otherwise.

Definition at line 365 of file mutexed.hpp.

◆ wait_for()

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<class Rep , class Period , typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
bool llh::mutexed::Mutexed< T, M, H >::wait_for ( std::chrono::duration< Rep, Period > const &  rel_time,
Predicate &&  p 
) const
inline

Waits until this is notified and the provided predicate returns true or until the specified duration has been spent waiting.

Calling this function is blocking.

The predicate is called with the inner value shared-locked if it is shared_lockable , and unique-locked otherwise.

Definition at line 377 of file mutexed.hpp.

◆ wait_until()

template<typename T , typename M = std::shared_mutex, typename H = no_cv>
template<class Clock , class Duration , typename Predicate >
requires std::is_same_v<H, has_cv> && invokable_with<Predicate, T const&>
bool llh::mutexed::Mutexed< T, M, H >::wait_until ( std::chrono::time_point< Clock, Duration > const &  timeout_time,
Predicate &&  p 
) const
inline

Waits until this is notified and the provided predicate returns true or until the specified time point has been passed.

Calling this function is blocking.

The predicate is called with the inner value shared-locked if it is shared_lockable , and unique-locked otherwise.

Definition at line 389 of file mutexed.hpp.