#include <mutex>
#include <condition_variable>
#include <thread>
class Semaphore {
public:
Semaphore(int count_ = 0): count(count_) {}
void notify() {
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}
void wait() {
std::unique_lock<std::mutex> lock(mtx);
while (count == 0) {
cv.wait(lock);
}
count--;
}
private:
std::mutex mtx;
std::condition_variable cv;
int count;
};
https://www.zhihu.com/question/31555101
Semaphore
最新推荐文章于 2023-12-24 06:00:00 发布