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();