5#define __XTD_STD_INTERNAL__
7#undef __XTD_STD_INTERNAL__
8#if defined(__xtd__cpp_lib_semaphore)
12#include <condition_variable>
19 template<std::ptrdiff_t least_max_value = std::numeric_limits<std::ptrdiff_t>::max()>
20 class counting_semaphore {
22 static constexpr std::ptrdiff_t
max() noexcept {
return least_max_value;}
23 explicit counting_semaphore(std::ptrdiff_t desired = 0) : count_(desired) {}
24 counting_semaphore(
const counting_semaphore&) =
delete;
27 std::unique_lock<std::mutex>
lock(mutex_);
29 condition_.wait(lock);
34 bool try_acquire() noexcept {
35 std::unique_lock<std::mutex>
lock(mutex_);
43 bool try_acquire_for(
const std::chrono::milliseconds& duration) {
44 std::unique_lock<std::mutex>
lock(mutex_);
45 if (condition_.wait_for(lock, duration, [
this]() { return count_ > 0; })) {
52 template<
class clock_t,
class duration_t >
53 bool try_acquire_until(
const std::chrono::time_point<clock_t, duration_t>& timeout_time) {
54 return try_acquire_until(timeout_time, [
this] {
return count_ > 0;});
57 template<
class clock_t,
class duration_t,
class predicate_t >
58 bool try_acquire_until(
const std::chrono::time_point<clock_t, duration_t>& timeout_time, predicate_t stop_waiting) {
59 std::unique_lock<std::mutex>
lock(mutex_);
60 if (condition_.wait_until(lock, timeout_time, stop_waiting)) {
67 void release(std::ptrdiff_t update = 1) {
68 std::lock_guard<std::mutex>
lock(mutex_);
69 for (std::ptrdiff_t index = 0; index < update; ++index) {
71 condition_.notify_one();
75 std::ptrdiff_t available()
const {
76 std::lock_guard<std::mutex>
lock(mutex_);
81 mutable std::mutex mutex_;
82 std::condition_variable condition_;
83 std::ptrdiff_t count_;
86 using binary_semaphore = std::counting_semaphore<1>;
Contains __xtd_std_version definitions.
xtd::threading::lock_guard lock
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock.h:22
@ release
Build type release.