CTimerThread now allows zero and negative timeouts. a negative

timeout never times out and CTimerThread is a no-op.
This commit is contained in:
crs 2002-06-09 22:20:01 +00:00
parent db8165db09
commit 30a6a8b837
2 changed files with 19 additions and 9 deletions

View File

@ -10,14 +10,20 @@
CTimerThread::CTimerThread(double timeout) : m_timeout(timeout)
{
assert(m_timeout > 0.0);
if (m_timeout >= 0.0) {
m_callingThread = new CThread(CThread::getCurrentThread());
m_timingThread = new CThread(new TMethodJob<CTimerThread>(
this, &CTimerThread::timer));
}
else {
m_callingThread = NULL;
m_timingThread = NULL;
}
}
CTimerThread::~CTimerThread()
{
if (m_timingThread != NULL) {
log((CLOG_DEBUG1 "cancelling timeout"));
m_timingThread->cancel();
m_timingThread->wait();
@ -25,6 +31,7 @@ CTimerThread::~CTimerThread()
delete m_timingThread;
delete m_callingThread;
}
}
void CTimerThread::timer(void*)
{

View File

@ -7,6 +7,9 @@ class CThread;
class CTimerThread {
public:
// cancels the calling thread after timeout seconds unless destroyed
// before then. if timeout is less than zero then it never times
// out and is a no-op.
CTimerThread(double timeout);
~CTimerThread();