C++ 参考手册

位置:首页 > C++ 参考手册 >C++ 标准库头文件 > 标准库头文件 <semaphore>

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

实现非负资源计数的信号量
(类模板)
仅拥有二个状态的信号量
(typedef)

概要

namespace std {
  template<ptrdiff_t LeastMaxValue = /* 实现定义 */>
    class counting_semaphore;
 
  using binary_semaphore = counting_semaphore<1>;
}

类模板 std::counting_semaphore

namespace std {
  template<ptrdiff_t LeastMaxValue = /* 实现定义 */>
  class counting_semaphore {
  public:
    static constexpr ptrdiff_t max() noexcept;
 
    constexpr explicit counting_semaphore(ptrdiff_t desired);
    ~counting_semaphore();
 
    counting_semaphore(const counting_semaphore&) = delete;
    counting_semaphore& operator=(const counting_semaphore&) = delete;
 
    void release(ptrdiff_t update = 1);
    void acquire();
    bool try_acquire() noexcept;
    template<class Rep, class Period>
      bool try_acquire_for(const chrono::duration<Rep, Period>& rel_time);
    template<class Clock, class Duration>
      bool try_acquire_until(const chrono::time_point<Clock, Duration>& abs_time);
 
  private:
    ptrdiff_t counter;          // 仅用于阐释
  };
}