You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libpEpAdapter/Semaphore.hh

38 lines
696 B
C++

#include <mutex>
#include <condition_variable>
namespace pEp {
class Semaphore {
3 years ago
std::mutex mtx;
std::condition_variable cv;
bool _stop;
public:
3 years ago
Semaphore() : _stop(false) { }
void stop()
{
std::unique_lock<std::mutex> lock(mtx);
_stop = true;
}
void try_wait()
{
std::unique_lock<std::mutex> lock(mtx);
if (!_stop)
return;
3 years ago
while(_stop)
cv.wait(lock);
}
void go()
{
std::unique_lock<std::mutex> lock(mtx);
_stop = false;
cv.notify_all();
}
};
}