barrier/mt/CTimerThread.cpp

43 lines
897 B
C++
Raw Normal View History

2001-10-06 14:13:28 +00:00
#include "CTimerThread.h"
#include "CThread.h"
#include "TMethodJob.h"
#include "CLog.h"
2001-10-06 14:13:28 +00:00
#include <assert.h>
//
// CTimerThread
//
CTimerThread::CTimerThread(double timeout) : m_timeout(timeout)
{
if (m_timeout >= 0.0) {
m_callingThread = new CThread(CThread::getCurrentThread());
m_timingThread = new CThread(new TMethodJob<CTimerThread>(
2001-10-06 14:13:28 +00:00
this, &CTimerThread::timer));
}
else {
m_callingThread = NULL;
m_timingThread = NULL;
}
2001-10-06 14:13:28 +00:00
}
CTimerThread::~CTimerThread()
{
if (m_timingThread != NULL) {
log((CLOG_DEBUG1 "cancelling timeout"));
m_timingThread->cancel();
m_timingThread->wait();
log((CLOG_DEBUG1 "cancelled timeout"));
delete m_timingThread;
delete m_callingThread;
}
2001-10-06 14:13:28 +00:00
}
void CTimerThread::timer(void*)
{
log((CLOG_DEBUG1 "timeout in %f seconds", m_timeout));
2001-10-06 14:13:28 +00:00
CThread::sleep(m_timeout);
log((CLOG_DEBUG1 "timeout"));
2001-10-06 14:13:28 +00:00
m_callingThread->cancel();
}