static std::thread l_TimerThread;
static bool l_StopTimerThread;
static TimerSet l_Timers;
+static int l_AliveTimers = 0;
/**
* Destructor for the Timer class.
void Timer::Initialize()
{
boost::mutex::scoped_lock lock(l_TimerMutex);
+
+ if (l_AliveTimers > 0) {
+ InitializeThread();
+ }
+}
+
+void Timer::Uninitialize()
+{
+ boost::mutex::scoped_lock lock(l_TimerMutex);
+
+ if (l_AliveTimers > 0) {
+ UninitializeThread();
+ }
+}
+
+void Timer::InitializeThread()
+{
l_StopTimerThread = false;
l_TimerThread = std::thread(&Timer::TimerThreadProc);
}
-void Timer::Uninitialize()
+void Timer::UninitializeThread()
{
{
- boost::mutex::scoped_lock lock(l_TimerMutex);
l_StopTimerThread = true;
l_TimerCV.notify_all();
}
+ l_TimerMutex.unlock();
+
if (l_TimerThread.joinable())
l_TimerThread.join();
+
+ l_TimerMutex.lock();
}
/**
{
boost::mutex::scoped_lock lock(l_TimerMutex);
m_Started = true;
+
+ if (++l_AliveTimers == 1) {
+ InitializeThread();
+ }
}
InternalReschedule(false);
boost::mutex::scoped_lock lock(l_TimerMutex);
+ if (m_Started && --l_AliveTimers == 0) {
+ UninitializeThread();
+ }
+
m_Started = false;
l_Timers.erase(this);
static void Initialize();
static void Uninitialize();
+ static void InitializeThread();
+ static void UninitializeThread();
void SetInterval(double interval);
double GetInterval() const;