命名空间
变体
操作

标准库头文件 <mutex> (C++11)

来自 cppreference.com
< cpp‎ | header
 
 
标准库头文件
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用实用程序
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

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

内容

(C++11)
提供基本的互斥功能
(class) [编辑]
提供互斥功能,该功能实现具有超时功能的锁定
(class) [编辑]
提供互斥功能,该功能可以被同一线程递归地锁定
(class) [编辑]
提供互斥功能,该功能可以递归地锁定
由同一线程实现,并使用超时进行锁定。
(类) [编辑]
实现严格基于作用域的互斥锁所有权包装器。
(类模板) [编辑]
实现可移动的互斥锁所有权包装器。
(类模板) [编辑]
用于多个互斥锁的死锁避免 RAII 包装器。
(类模板) [编辑]
(C++11)
辅助对象,用于确保 call_once 只调用一次函数。
(类) [编辑]

函数

(C++11)
尝试通过重复调用 try_lock 获取互斥锁的所有权。
(函数模板) [编辑]
(C++11)
锁定指定的互斥锁,如果任何一个不可用,则会阻塞。
(函数模板) [编辑]
(C++11)
即使从多个线程调用,也只调用一次函数。
(函数模板) [编辑]
专门化 std::swap 算法。
(函数模板) [编辑]

标签

用于指定锁定策略的标签。
(标签)[编辑]

[编辑] 概要

namespace std {
  class mutex;
  class recursive_mutex;
  class timed_mutex;
  class recursive_timed_mutex;
 
  struct defer_lock_t { explicit defer_lock_t() = default; };
  struct try_to_lock_t { explicit try_to_lock_t() = default; };
  struct adopt_lock_t { explicit adopt_lock_t() = default; };
 
  inline constexpr defer_lock_t  defer_lock { };
  inline constexpr try_to_lock_t try_to_lock { };
  inline constexpr adopt_lock_t  adopt_lock { };
 
  template<class Mutex> class lock_guard;
  template<class... MutexTypes> class scoped_lock;
  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

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

[编辑] std::recursive_mutex

namespace std {
  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();
 
    using native_handle_type = /* implementation-defined */;
    native_handle_type native_handle();
  };
}

[编辑] std::timed_mutex

namespace std {
  class timed_mutex {
  public:
    timed_mutex();
    ~timed_mutex();
 
    timed_mutex(const timed_mutex&) = delete;
    timed_mutex& operator=(const timed_mutex&) = delete;
 
    void lock();    // blocking
    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();
 
    using native_handle_type = /* implementation-defined */;
    native_handle_type native_handle();
  };
}

[编辑] std::recursive_timed_mutex

namespace std {
  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();    // blocking
    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();
 
    using native_handle_type = /* implementation-defined */;
    native_handle_type native_handle();
  };
}

[编辑] 类模板 std::lock_guard

namespace std {
  template<class Mutex>
  class lock_guard {
  public:
    using mutex_type = Mutex;
 
    explicit lock_guard(mutex_type& m);
    lock_guard(mutex_type& m, adopt_lock_t);
    ~lock_guard();
 
    lock_guard(const lock_guard&) = delete;
    lock_guard& operator=(const lock_guard&) = delete;
 
  private:
    mutex_type& pm;             // exposition only
  };
}

[编辑] 类模板 std::scoped_lock

namespace std {
  template<class... MutexTypes>
  class scoped_lock {
  public:
    using mutex_type = Mutex;   // If MutexTypes... consists of the single type 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;   // exposition only
  };
}

[编辑] 类模板 std::unique_lock

namespace std {
  template<class Mutex>
  class unique_lock {
  public:
    using mutex_type = Mutex;
 
    // construct/copy/destroy
    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(const unique_lock&) = delete;
    unique_lock& operator=(const unique_lock&) = delete;
 
    unique_lock(unique_lock&& u) noexcept;
    unique_lock& operator=(unique_lock&& u);
 
    // locking
    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();
 
    // modifiers
    void swap(unique_lock& u) noexcept;
    mutex_type* release() noexcept;
 
    // observers
    bool owns_lock() const noexcept;
    explicit operator bool () const noexcept;
    mutex_type* mutex() const noexcept;
 
  private:
    mutex_type* pm;             // exposition only
    bool owns;                  // exposition only
  };
 
  template<class Mutex>
    void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
}

[编辑] std::once_flag

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