#ifndef COUNTING_SEMAPHORE_H #define COUNTING_SEMAPHORE_H #include #include #include #include #include #include #include class CountingSemaphore { public: static CountingSemaphore& getInstance() { static CountingSemaphore instance; return instance; } // Delete copy constructor and assignment operator CountingSemaphore(const CountingSemaphore&) = delete; CountingSemaphore& operator=(const CountingSemaphore&) = delete; void acquire() { std::unique_lock lock(mtx_); cv_.wait(lock, [this]() { return count_ > 0; }); --count_; } void release() { std::lock_guard lock(mtx_); ++count_; if (count_ <= max_count_) { cv_.notify_one(); } } private: CountingSemaphore() : max_count_(std::max(1u, static_cast(0.95 * std::thread::hardware_concurrency()))), count_(max_count_) { } std::mutex mtx_; std::condition_variable cv_; const uint max_count_; uint count_; }; #endif