ManualResetEvent.h
// ManualResetEvent.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2013
#pragma once
#ifndef Ceda_cxMessage_ManualResetEvent_H
#define Ceda_cxMessage_ManualResetEvent_H
#include "cxMessage.h"
#include "Ceda/cxUtils/CedaAssert.h"
#include <mutex>
#include <condition_variable>
namespace ceda
{
class ManualResetEvent
{
cxNotCloneable(ManualResetEvent)
public:
explicit ManualResetEvent(bool signaled = false) : signaled_(signaled) {}
void Signal()
{
std::lock_guard<std::mutex> lock(mutex_);
signaled_ = true;
cv_.notify_all();
}
void Reset()
{
std::lock_guard<std::mutex> lock(mutex_);
signaled_ = false;
}
void Wait()
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait(lock,[this]{return signaled_;});
}
// Wait with a timeout.
// Returns true if the wait was completed successfully because the event had been signalled
bool Wait(uint32 msec)
{
std::unique_lock<std::mutex> lock(mutex_);
return cv_.wait_for(lock,std::chrono::milliseconds(msec),[this]{return signaled_;});
}
private:
std::mutex mutex_;
std::condition_variable cv_;
bool signaled_;
};
} // namespace ceda
#endif // include guard