// // Created by zx on 22-12-1. // #pragma once #include #include template class TimedLockData { public: TimedLockData(); TimedLockData& operator=(const T& data); bool timeout(const double &timeout = 0.2); T operator()(void); protected: T data_; std::chrono::steady_clock::time_point tp_; std::mutex mutex_; }; template TimedLockData::TimedLockData() { } template TimedLockData& TimedLockData::operator=(const T& data){ std::lock_guard lck (mutex_); data_=data; tp_=std::chrono::steady_clock::now(); return *this; } template bool TimedLockData::timeout(const double &timeout) { auto now=std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast(now - tp_); double time = double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den; return time>timeout; } template T TimedLockData::operator()(void) { std::lock_guard lck (mutex_); return data_; }