forked from pEp.foundation/libpEpAdapter
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
696 B
38 lines
696 B
#include <mutex>
|
|
#include <condition_variable>
|
|
|
|
namespace pEp {
|
|
class Semaphore {
|
|
std::mutex mtx;
|
|
std::condition_variable cv;
|
|
bool _stop;
|
|
|
|
public:
|
|
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;
|
|
|
|
while(_stop)
|
|
cv.wait(lock);
|
|
}
|
|
|
|
void go()
|
|
{
|
|
std::unique_lock<std::mutex> lock(mtx);
|
|
_stop = false;
|
|
cv.notify_all();
|
|
}
|
|
};
|
|
}
|
|
|