From 30a6a8b83750345fdea7ef7b3407ac1018e91f0c Mon Sep 17 00:00:00 2001 From: crs Date: Sun, 9 Jun 2002 22:20:01 +0000 Subject: [PATCH] CTimerThread now allows zero and negative timeouts. a negative timeout never times out and CTimerThread is a no-op. --- mt/CTimerThread.cpp | 25 ++++++++++++++++--------- mt/CTimerThread.h | 3 +++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/mt/CTimerThread.cpp b/mt/CTimerThread.cpp index 0a3e8542..786ed8df 100644 --- a/mt/CTimerThread.cpp +++ b/mt/CTimerThread.cpp @@ -10,20 +10,27 @@ CTimerThread::CTimerThread(double timeout) : m_timeout(timeout) { - assert(m_timeout > 0.0); - m_callingThread = new CThread(CThread::getCurrentThread()); - m_timingThread = new CThread(new TMethodJob( + if (m_timeout >= 0.0) { + m_callingThread = new CThread(CThread::getCurrentThread()); + m_timingThread = new CThread(new TMethodJob( this, &CTimerThread::timer)); + } + else { + m_callingThread = NULL; + m_timingThread = NULL; + } } CTimerThread::~CTimerThread() { - log((CLOG_DEBUG1 "cancelling timeout")); - m_timingThread->cancel(); - m_timingThread->wait(); - log((CLOG_DEBUG1 "cancelled timeout")); - delete m_timingThread; - delete m_callingThread; + 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; + } } void CTimerThread::timer(void*) diff --git a/mt/CTimerThread.h b/mt/CTimerThread.h index 817642d3..118fde1d 100644 --- a/mt/CTimerThread.h +++ b/mt/CTimerThread.h @@ -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();