Documentation Home

The std::shared_timed_mutex class provides support for locks with shared ownership as well as the exclusive-ownership locks provided by std::timed_mutex. Prior to modifying the data protected by the mutex, the mutex must be locked by calling lock(), try_lock(), try_lock_for() or try_lock_until(); if shared lock ownership is required (for a read-only operation) then such a lock can be acquired by calling lock_shared(), try_lock_shared(), try_lock_shared_for() or try_lock_shared_until().

If either an exclusive or shared lock is already held by another thread then an attempt to acquire an exclusive lock will fail (try_lock()), block until the lock can be acquired (lock()), or block until the lock can be acquired or the lock attempt times out (try_lock_for() or try_lock_until()).

Once an exclusive lock has been acquired (whichever function was used to acquire it) it must be released by calling unlock() before another thread can acquire the lock on the mutex.

Multiple threads may hold a shared-ownership lock on the same mutex simultaneously. However, if another thread holds an exclusive lock, or is blocked in an attempt to acquire an exclusive lock, an attempt to acquire a shared lock will fail (try_lock_shared()), block until the lock can be acquired (lock_shared()), or block until the lock can be acquired or the lock attempt times out (try_lock_shared_for() or try_lock_shared_until()).

std::shared_timed_mutex meets the SharedLockable requirements.

For details on the members, see std::shared_timed_mutex class members.

class shared_timed_mutex
{
public:
    shared_timed_mutex(shared_timed_mutex const&)=delete;
    shared_timed_mutex& operator=(shared_timed_mutex const&)=delete;

    shared_timed_mutex();
    ~shared_timed_mutex();

    void lock();
    void unlock();
    bool try_lock();

    template<typename Rep,typename Period>
    bool try_lock_for(
        std::chrono::duration<Rep,Period> const&
        relative_time);

    template<typename Clock,typename Duration>
    bool try_lock_until(
        std::chrono::time_point<Clock,Duration> const&
        absolute_time);

    void lock_shared();
    void unlock_shared();
    bool try_lock_shared();

    template<typename Rep,typename Period>
    bool try_lock_shared_for(
        std::chrono::duration<Rep,Period> const&
        relative_time);

    template<typename Clock_shared,typename Duration>
    bool try_lock_shared_until(
        std::chrono::time_point<Clock_shared,Duration> const&
        absolute_time);
};
Header

#include <shared_mutex>

See Also