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.
40 lines
723 B
40 lines
723 B
3 years ago
|
#include <mutex>
|
||
|
#include <condition_variable>
|
||
|
|
||
|
namespace pEp {
|
||
|
class Semaphore {
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
std::mutex mtx;
|
||
|
std::condition_variable cv;
|
||
|
bool _stop;
|
||
|
};
|
||
|
}
|
||
|
|