31 lines
498 B
C++
31 lines
498 B
C++
#pragma once
|
|
|
|
#include "core_global.h"
|
|
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
|
|
namespace paysages {
|
|
namespace system {
|
|
|
|
class Semaphore {
|
|
public:
|
|
Semaphore(int resources);
|
|
|
|
inline void acquire() {
|
|
unique_lock lk(mex);
|
|
cond.wait(lk, [this]{ return resources > 0; });
|
|
}
|
|
inline void release() {
|
|
lock_guard lk(mex);
|
|
resources++;
|
|
cond.notify_one();
|
|
}
|
|
private:
|
|
int resources;
|
|
condition_variable cond;
|
|
mutex mex;
|
|
};
|
|
}
|
|
}
|