Don't need to recreate ThreadedTimer thread every time we need to restart.

ms-pipeline-optim-2
Mooneer Salem 2025-08-14 00:29:20 -07:00
parent feb4f4d528
commit 21297cf36f
3 changed files with 21 additions and 5 deletions

View File

@ -197,8 +197,7 @@ void SocketIoClient::handleEngineIoMessage_(char* ptr, int length)
// "ping" -- send pong
connection_->send("3");
pingTimer_.stop();
pingTimer_.start();
pingTimer_.restart();
break;
}
case '4':

View File

@ -28,6 +28,7 @@
ThreadedTimer::ThreadedTimer()
: isDestroying_(false)
, isRestarting_(false)
, repeat_(false)
, timeoutMilliseconds_(0)
{
@ -88,6 +89,19 @@ void ThreadedTimer::stop()
}
}
void ThreadedTimer::restart()
{
if (objectThread_.joinable())
{
isRestarting_ = true;
timerCV_.notify_one();
}
else
{
start();
}
}
void ThreadedTimer::eventLoop_()
{
#if defined(__APPLE__)
@ -98,9 +112,10 @@ void ThreadedTimer::eventLoop_()
do
{
std::unique_lock<std::mutex> lk(timerMutex_);
if (!timerCV_.wait_for(lk, std::chrono::milliseconds(timeoutMilliseconds_), [&]() { return isDestroying_; }) && fn_)
isRestarting_ = false;
if (!timerCV_.wait_for(lk, std::chrono::milliseconds(timeoutMilliseconds_), [&]() { return isDestroying_ || isRestarting_; }) && fn_)
{
fn_(*this);
}
} while (!isDestroying_ && repeat_);
} while (!isDestroying_ && (isRestarting_ || repeat_));
}

View File

@ -44,11 +44,13 @@ public:
void start();
void stop();
void restart();
bool isRunning();
private:
bool isDestroying_;
bool isRestarting_;
std::thread objectThread_;
std::mutex timerMutex_;
std::condition_variable timerCV_;
@ -59,4 +61,4 @@ private:
void eventLoop_();
};
#endif // THREADED_TIMER_H
#endif // THREADED_TIMER_H