map/das-dn/third_party/AdsLib/Semaphore.h
2024-12-03 10:36:06 +08:00

32 lines
593 B
C++

// SPDX-License-Identifier: MIT
/**
Copyright (c) 2015 - 2020 Beckhoff Automation GmbH & Co. KG
Author: Patrick Bruenn <p.bruenn@beckhoff.com>
*/
#pragma once
#include <condition_variable>
#include <mutex>
struct Semaphore {
void release()
{
std::unique_lock<std::mutex> lock(mutex);
++count;
cv.notify_one();
}
void acquire()
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&](){return count > 0; });
--count;
}
private:
int count = 0;
std::mutex mutex;
std::condition_variable cv;
};