Skip to content

Commit

Permalink
fix PeriodicThread race
Browse files Browse the repository at this point in the history
  • Loading branch information
DaAwesomeP committed Apr 16, 2023
1 parent 100f644 commit afefe6d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/aniray/PeriodicThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class PeriodicThread { // NOLINT(cppcoreguidelines-special-member-functions,hicp
std::unique_ptr<boost::asio::steady_timer> mTimer;
std::unique_ptr<boost::thread> mIOThread;
mutable std::shared_mutex mUpdateRateMutex;
mutable std::shared_mutex mHandlerMutex;

void timerHandler();
};
Expand Down
4 changes: 3 additions & 1 deletion src/PeriodicThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ void PeriodicThread::updateRate(std::chrono::milliseconds updateRateMs) {
}

PeriodicThread::~PeriodicThread() {
const std::unique_lock<std::shared_mutex> lock(mHandlerMutex);
stop();
// if (mIOThread->joinable()) {
// mIOThread->join();
// }
}

void PeriodicThread::timerHandler () {
const std::shared_lock<std::shared_mutex> lockHandler(mHandlerMutex);
periodicAction();
const std::shared_lock<std::shared_mutex> lock(mUpdateRateMutex);
const std::shared_lock<std::shared_mutex> lockUpdateRate(mUpdateRateMutex);
mTimer->expires_at(mTimer->expiry() + mUpdateRateMs);
mTimer->async_wait([this] (const boost::system::error_code&) { timerHandler(); });
}
Expand Down

0 comments on commit afefe6d

Please sign in to comment.