标准库头文件 <mutex>

来自cppreference.com
< cpp‎ | header
 
 
 

此头文件是线程支持库的一部分。

目录

(C++11)
提供基本互斥设施
(类) [编辑]
提供互斥设施,实现有时限锁定
(类) [编辑]
提供能被同一线程递归锁定的互斥设施
(类) [编辑]
提供能被同一线程递归锁定的互斥设施,并实现有时限锁定
(类) [编辑]
实现严格基于作用域的互斥所有权包装器
(类模板) [编辑]
实现可移动的互斥锁有权包装器
(类模板) [编辑]
多互斥的免死锁 RAII 封装器
(类模板) [编辑]
用于指定锁定策略的标签类型
(类) [编辑]
用于指定锁定策略的标签常量
(常量) [编辑]
(C++11)
确保 call_once 只调用函数一次的帮助对象
(类) [编辑]

函数

(C++11)
试图通过重复调用 try_lock 获得互斥的所有权
(函数模板) [编辑]
(C++11)
锁定互斥,若有任何不可用者则阻塞
(函数模板) [编辑]
(C++11)
仅调用函数一次,即使从多个线程调用
(函数模板) [编辑]
std::swapunique_lock 的特化
(函数模板) [编辑]

[编辑] 概要

namespace std {
    class mutex;
    class recursive_mutex;
    class timed_mutex;
    class recursive_timed_mutex;
 
    struct defer_lock_t { explicit defer_lock_t() = default; };
    inline constexpr defer_lock_t defer_lock { };
 
    struct try_to_lock_t { explicit try_to_lock_t() = default; };
    inline constexpr try_to_lock_t try_to_lock { };
 
    struct adopt_lock_t { explicit adopt_lock_t() = default; };
    inline constexpr adopt_lock_t adopt_lock { };
 
    template <class Mutex> class lock_guard;
    template <class Mutex> class unique_lock;
 
    template <class Mutex>
    void swap(unique_lock<Mutex>& x,
              unique_lock<Mutex>& y) noexcept;
 
    template <class L1, class L2, class... L3>
    int try_lock(L1&, L2&, L3&...);
 
    template <class L1, class L2, class... L3>
    void lock(L1&, L2&, L3&...);
 
    struct once_flag;
 
    template<class Callable, class ...Args>
    void call_once(once_flag& flag, Callable func, Args&&... args);
}

[编辑] std::mutex

class mutex {
 public:
    constexpr mutex() noexcept;
    ~mutex();
    mutex(const mutex&) = delete;
    mutex& operator=(const mutex&) = delete;
 
    void lock();
    bool try_lock();
    void unlock();
    typedef /*implementation-defined*/ native_handle_type;
    native_handle_type native_handle();
};

[编辑] std::recursive_mutex

class recursive_mutex {
 public:
    recursive_mutex();
    ~recursive_mutex();
    recursive_mutex(const recursive_mutex&) = delete;
    recursive_mutex& operator=(const recursive_mutex&) = delete;
 
    void lock();
    bool try_lock() noexcept;
    void unlock();
    typedef /*implementation-defined*/ native_handle_type;
    native_handle_type native_handle();
};

[编辑] std::timed_mutex

class timed_mutex {
 public:
    timed_mutex();
    ~timed_mutex();
    timed_mutex(const timed_mutex&) = delete;
    timed_mutex& operator=(const timed_mutex&) = delete;
 
    void lock();
    bool try_lock();
    template <class Rep, class Period>
        bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
    template <class Clock, class Duration>
        bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
    void unlock();
    typedef /*implementation-defined*/ native_handle_type;
    native_handle_type native_handle();
};

[编辑] std::recursive_timed_mutex

class recursive_timed_mutex {
 public:
    recursive_timed_mutex();
    ~recursive_timed_mutex();
    recursive_timed_mutex(const recursive_timed_mutex&) = delete;
    recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
 
    void lock();
    bool try_lock() noexcept;
    template <class Rep, class Period>
        bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
    template <class Clock, class Duration>
        bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
    void unlock();
    typedef /*implementation-defined*/ native_handle_type;
    native_handle_type native_handle();
};

[编辑] std::lock_guard

template <class Mutex>
class lock_guard {
 public:
    typedef Mutex mutex_type;
    explicit lock_guard(mutex_type& m);
    lock_guard(mutex_type& m, adopt_lock_t);
    ~lock_guard();
    lock_guard(lock_guard const&) = delete;
    lock_guard& operator=(lock_guard const&) = delete;
 private:
    mutex_type& pm; // 仅为说明
};

[编辑] std::scoped_lock

template<class... MutexTypes>
class scoped_lock {
public:
    using mutex_type = Mutex; // 若 MutexTypes... 仅由单个类型 Mutex 组成
 
    explicit scoped_lock(MutexTypes&... m);
    explicit scoped_lock(adopt_lock_t, MutexTypes&... m);
    ~scoped_lock();
 
    scoped_lock(const scoped_lock&) = delete;
    scoped_lock& operator=(const scoped_lock&) = delete;
 
private:
    tuple<MutexTypes&...> pm; // 仅为说明
};

[编辑] std::unique_lock

template <class Mutex>
class unique_lock {
 public:
    typedef Mutex mutex_type;
 
    // 构造/复制/销毁:
    unique_lock() noexcept;
    explicit unique_lock(mutex_type& m);
    unique_lock(mutex_type& m, defer_lock_t) noexcept;
    unique_lock(mutex_type& m, try_to_lock_t);
    unique_lock(mutex_type& m, adopt_lock_t);
    template <class Clock, class Duration>
    unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
    template <class Rep, class Period>
    unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
    ~unique_lock();
    unique_lock(unique_lock const&) = delete;
    unique_lock& operator=(unique_lock const&) = delete;
    unique_lock(unique_lock&& u) noexcept;
    unique_lock& operator=(unique_lock&& u) noexcept;
 
    // 锁定:
    void lock();
    bool try_lock();
    template <class Rep, class Period>
    bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
    template <class Clock, class Duration>
    bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
    void unlock();
 
    // 修改器:
    void swap(unique_lock& u) noexcept;
    mutex_type *release() noexcept;
 
    // 观察器:
    bool owns_lock() const noexcept;
    explicit operator bool () const noexcept;
    mutex_type* mutex() const noexcept;
 
 private:
    mutex_type *pm; // 仅为说明
    bool owns; // 仅为说明
};

[编辑] std::once_flag

struct once_flag {
    constexpr once_flag() noexcept;
    once_flag(const once_flag&) = delete;
    once_flag& operator=(const once_flag&) = delete;
};