Total Articles 180
[Header]
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
// ----------------------------------------------------------------------------
// Event
// ----------------------------------------------------------------------------
class Event
{
protected:
boost::mutex* m_mutex;
boost::condition* m_cond;
bool m_manualReset;
bool m_state;
public:
Event(bool manualReset = false, bool initialState = false);
virtual ~Event();
public:
void setEvent();
void resetEvent();
bool wait(Duration timeout = INFINITE);
};
[cpp]
Event::Event(bool manualReset, bool initialState)
{
m_mutex = new boost::mutex;
m_cond = new boost::condition;
m_manualReset = manualReset;
m_state = initialState;
}
Event::~Event()
{
SAFE_DELETE(m_mutex);
SAFE_DELETE(m_cond);
}
void Event::setEvent()
{
boost::mutex::scoped_lock lock(*m_mutex);
m_state = true;
m_cond->notify_all();
}
void Event::resetEvent()
{
m_state = false;
}
bool Event::wait(Duration timeout)
{
boost::mutex::scoped_lock lock(*m_mutex);
bool res = true;
if (!m_state)
{
if (timeout == INFINITE)
{
m_cond->wait(lock);
} else
{
boost::posix_time::time_duration td = boost::posix_time::milliseconds(timeout);
res = m_cond->timed_wait(lock, td);
}
}
if (!m_manualReset) m_state = false;
return res;
}