标准库标头 <condition_variable> (C++11)

来自cppreference.com
< cpp‎ | header


 
 
标准库头
通用工具
<any> (C++17)
<bitset>
<bit> (C++20)
<charconv> (C++17)
<expected> (C++23)
<format> (C++20)
<functional>
<optional> (C++17)
<stdbit.h> (C++26)
<tuple> (C++11)
<typeindex> (C++11)
<utility>
<variant> (C++17)
容器
<array> (C++11)
<deque>
<flat_map> (C++23)
<flat_set> (C++23)
<forward_list> (C++11)
<hive> (C++26)
<inplace_vector> (C++26)   
<list>
<map>
<mdspan> (C++23)
<queue>
<set>
<span> (C++20)
<stack>
<unordered_map> (C++11)
<unordered_set> (C++11)
<vector>
迭代器
<iterator>
范围
<generator> (C++23)
<ranges> (C++20)
输入/输出
<cinttypes> (C++11)
<cstdio>
<filesystem> (C++17)
<fstream>
<iomanip>
<iosfwd>
<iostream>
<ios>
<istream>
<ostream>
<print> (C++23)
<spanstream> (C++23)
<sstream>
<streambuf>
<strstream> (C++98/26*)
<syncstream> (C++20)
并发支持
<atomic> (C++11)
<barrier> (C++20)
<condition_variable> (C++11)
<future> (C++11)
<hazard_pointer> (C++26)
<latch> (C++20)
<mutex> (C++11)
<rcu> (C++26)
<semaphore> (C++20)
<shared_mutex> (C++14)
<stdatomic.h> (C++23)
<stop_token> (C++20)
<thread> (C++11)
执行支持
<execution> (C++17)




 

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

目录

提供与 std::unique_lock 关联的条件变量
(类) [编辑]
提供与任何锁类型关联的条件变量
(类) [编辑]
(C++11)
列出条件变量上定时等待的可能结果
(枚举) [编辑]

函数

安排当此线程完全结束时调用一次 notify_all
(函数) [编辑]

[编辑] 概要

namespace std {
  class condition_variable;
  class condition_variable_any;
 
  void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
 
  enum class cv_status { no_timeout, timeout };
}

[编辑] std::condition_variable

namespace std {
  class condition_variable {
  public:
    condition_variable();
    ~condition_variable();
 
    condition_variable(const condition_variable&) = delete;
    condition_variable& operator=(const condition_variable&) = delete;
 
    void notify_one() noexcept;
    void notify_all() noexcept;
    void wait(unique_lock<mutex>& lock);
    template<class Pred>
      void wait(unique_lock<mutex>& lock, Pred pred);
    template<class Clock, class Duration>
      cv_status wait_until(unique_lock<mutex>& lock,
                           const chrono::time_point<Clock, Duration>& abs_time);
    template<class Clock, class Duration, class Pred>
      bool wait_until(unique_lock<mutex>& lock,
                      const chrono::time_point<Clock, Duration>& abs_time, Pred pred);
    template<class Rep, class Period>
      cv_status wait_for(unique_lock<mutex>& lock,
                         const chrono::duration<Rep, Period>& rel_time);
    template<class Rep, class Period, class Pred>
      bool wait_for(unique_lock<mutex>& lock,
                    const chrono::duration<Rep, Period>& rel_time, Pred pred);
 
    using native_handle_type = /* 由实现定义 */;
    native_handle_type native_handle();
  };
}

[编辑] std::condition_variable_any

namespace std {
  class condition_variable_any {
  public:
    condition_variable_any();
    ~condition_variable_any();
 
    condition_variable_any(const condition_variable_any&) = delete;
    condition_variable_any& operator=(const condition_variable_any&) = delete;
 
    void notify_one() noexcept;
    void notify_all() noexcept;
 
    // 不可中断等待
    template<class Lock>
      void wait(Lock& lock);
    template<class Lock, class Pred>
      void wait(Lock& lock, Pred pred);
 
    template<class Lock, class Clock, class Duration>
      cv_status wait_until(Lock& lock,
                           const chrono::time_point<Clock, Duration>& abs_time);
    template<class Lock, class Clock, class Duration, class Pred>
      bool wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time,
                      Pred pred);
    template<class Lock, class Rep, class Period>
      cv_status wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time);
    template<class Lock, class Rep, class Period, class Pred>
      bool wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time, Pred pred);
 
    // 可中断等待
    template<class Lock, class Pred>
      bool wait(Lock& lock, stop_token stoken, Pred pred);
    template<class Lock, class Clock, class Duration, class Pred>
      bool wait_until(Lock& lock, stop_token stoken,
                      const chrono::time_point<Clock, Duration>& abs_time, Pred pred);
    template<class Lock, class Rep, class Period, class Pred>
      bool wait_for(Lock& lock, stop_token stoken,
                    const chrono::duration<Rep, Period>& rel_time, Pred pred);
  };
}