Files
ARBKVS/P5/Semaphor.h
2024-01-22 23:26:29 +01:00

31 lines
644 B
C++

#include <mutex>
#include <condition_variable>
#include <queue>
class Semaphor
{
private:
int count;
std::mutex mtx;
std::condition_variable cond;
public:
Semaphor(int i) : count(i) {}
void acquire()
{
std::unique_lock<std::mutex> lock(mtx);
while (count == 0)
{ // Solange keine Ressourcen verfügbar sind, warten
cond.wait(lock);
}
count--; // Ressource belegen
}
void release()
{
std::unique_lock<std::mutex> lock(mtx);
count++; // Ressource freigeben
cond.notify_one(); // Einen wartenden Thread aufwecken
}
};