Anpassungen Semaphor

This commit is contained in:
S170H
2024-01-21 00:03:33 +01:00
parent 6655d01102
commit fcdc4d17c1
8 changed files with 93 additions and 93 deletions

32
P5/Semaphor.h Normal file
View File

@@ -0,0 +1,32 @@
#include <mutex>
#include <condition_variable>
#include <queue>
class Semaphor
{
private:
int count;
std::mutex mtx;
std::condition_variable cond;
std::queue<std::thread::id> wait_queue; // Beispielhafte Warteschlange
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
}
};