#ifndef SEMAPHORE_H
#define SEMAPHORE_H
#include<mutex>
#include<condition_variable>
class Semaphore{
public:
Semaphore(int resLimit):_resLimit(resLimit){}
~Semaphore()=default;
void wait()
{
std::unique_lock<std::mutex> lock(_mtx);
_condMtx.wait(lock,
[this](){
return _resLimit>0;
});
_resLimit--;
}
void post()
{
std::unique_lock<std::mutex> lock(_mtx);
_resLimit++;
_condMtx.notify_all();
}
private:
int _resLimit;
std::mutex _mtx;
std::condition_variable _condMtx;
};
#endif
05-27