#include <userver/engine/mutex.hpp>
std::mutex replacement for asynchronous tasks.
Ignores task cancellations (succeeds even if the current task is cancelled).
Example usage:
constexpr std::string_view kTestData = "Test Data";
{
const std::lock_guard<engine::Mutex> lock(mutex);
const auto x = kTestData;
ASSERT_EQ(kTestData, x);
}
- See also
- Synchronization Primitives
Definition at line 28 of file mutex.hpp.
|
| Mutex (const Mutex &)=delete |
|
| Mutex (Mutex &&)=delete |
|
Mutex & | operator= (const Mutex &)=delete |
|
Mutex & | operator= (Mutex &&)=delete |
|
void | lock () |
|
void | unlock () |
|
bool | try_lock () noexcept |
|
template<typename Rep, typename Period> |
bool | try_lock_for (const std::chrono::duration< Rep, Period > &) |
|
template<typename Clock, typename Duration> |
bool | try_lock_until (const std::chrono::time_point< Clock, Duration > &) |
|
bool | try_lock_until (Deadline deadline) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
|
◆ lock()
void engine::Mutex::lock |
( |
| ) |
|
Locks the mutex. Blocks current task if the mutex is locked by another task. Throws if a task tries to lock a mutex which is already locked by the current task.
- Note
- The method waits for the mutex even if the current task is cancelled.
◆ try_lock()
bool engine::Mutex::try_lock |
( |
| ) |
|
|
nodiscardnoexcept |
Tries to lock the mutex without blocking the task, returns true if succeeded.
- Note
- The behavior of the function is not affected by the cancellation requests.
◆ try_lock_for()
template<typename Rep, typename Period>
bool engine::Mutex::try_lock_for |
( |
const std::chrono::duration< Rep, Period > & | duration | ) |
|
|
nodiscard |
Tries to lock the mutex in specified duration. Blocks current task if the mutex is locked by another task up to the provided duration. Throws if a task tries to lock a mutex which is already locked by the current task.
- Returns
- true if the locking succeeded
- Note
- The method waits for the mutex even if the current task is cancelled.
Definition at line 90 of file mutex.hpp.
◆ try_lock_until()
template<typename Clock, typename Duration>
bool engine::Mutex::try_lock_until |
( |
const std::chrono::time_point< Clock, Duration > & | until | ) |
|
|
nodiscard |
Tries to lock the mutex till specified time point. Blocks current task if the mutex is locked by another task up to the provided time point. Throws if a task tries to lock a mutex which is already locked by the current task.
- Returns
- true if the locking succeeded
- Note
- The method waits for the mutex even if the current task is cancelled.
Definition at line 95 of file mutex.hpp.
◆ unlock()
void engine::Mutex::unlock |
( |
| ) |
|
Unlocks the mutex. Before calling this method the mutex should be locked by the current task.
- Note
- the order of tasks to unblock is unspecified. Any code assuming any specific order (e.g. FIFO) is incorrect and should be fixed.
The documentation for this class was generated from the following file: