// // Created by zx on 22-12-1. // #ifndef LIO_LIVOX_CPP_DEFINE_TIME_DATA_H_ #define LIO_LIVOX_CPP_DEFINE_TIME_DATA_H_ #include #include template class TimedLockData { public: TimedLockData(); void reset(const T& tdata,double timeout=0.1); bool timeout(); T Get(); protected: T data_; std::chrono::steady_clock::time_point tp_; std::mutex mutex_; double timeout_; }; template TimedLockData::TimedLockData() { timeout_=0.1; } template void TimedLockData::reset(const T& tdata,double timeout) { std::lock_guard lck (mutex_); data_=tdata; timeout_=timeout; tp_=std::chrono::steady_clock::now(); } template bool TimedLockData::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::Get() { std::lock_guard lck (mutex_); return data_; } #endif //LIO_LIVOX_CPP_DEFINE_TIME_DATA_H_