Limit max data size for async callback

This commit is contained in:
John Zhao 2018-07-11 15:04:14 +08:00
parent 0153261447
commit 3dc21c0378
3 changed files with 11 additions and 7 deletions

View File

@ -335,7 +335,7 @@ void Device::SetStreamCallback(
if (async) if (async)
stream_async_callbacks_[stream] = stream_async_callbacks_[stream] =
std::make_shared<stream_async_callback_t>( std::make_shared<stream_async_callback_t>(
to_string(stream), callback); to_string(stream), callback); // max_data_size = 1
} else { } else {
stream_callbacks_.erase(stream); stream_callbacks_.erase(stream);
stream_async_callbacks_.erase(stream); stream_async_callbacks_.erase(stream);
@ -347,7 +347,8 @@ void Device::SetMotionCallback(motion_callback_t callback, bool async) {
if (callback) { if (callback) {
if (async) if (async)
motion_async_callback_ = motion_async_callback_ =
std::make_shared<motion_async_callback_t>("motion", callback, true); std::make_shared<motion_async_callback_t>("motion", callback, 1000);
// will drop old motion datas after callback cost > 2 s (1000 / 500 Hz)
} else { } else {
motion_async_callback_ = nullptr; motion_async_callback_ = nullptr;
} }

View File

@ -31,7 +31,8 @@ class AsyncCallback {
public: public:
using callback_t = std::function<void(Data data)>; using callback_t = std::function<void(Data data)>;
AsyncCallback(std::string name, callback_t callback, bool concat = false); AsyncCallback(
std::string name, callback_t callback, std::size_t max_data_size = 0);
~AsyncCallback(); ~AsyncCallback();
void PushData(Data data); void PushData(Data data);
@ -50,8 +51,8 @@ class AsyncCallback {
std::thread thread_; std::thread thread_;
std::uint32_t count_; std::uint32_t count_;
bool concat_;
std::vector<Data> datas_; std::vector<Data> datas_;
std::size_t max_data_size_;
}; };
MYNTEYE_END_NAMESPACE MYNTEYE_END_NAMESPACE

View File

@ -24,11 +24,11 @@ MYNTEYE_BEGIN_NAMESPACE
template <class Data> template <class Data>
AsyncCallback<Data>::AsyncCallback( AsyncCallback<Data>::AsyncCallback(
std::string name, callback_t callback, bool concat) std::string name, callback_t callback, std::size_t max_data_size)
: name_(std::move(name)), : name_(std::move(name)),
callback_(std::move(callback)), callback_(std::move(callback)),
count_(0), count_(0),
concat_(concat) { max_data_size_(max_data_size) {
VLOG(2) << __func__; VLOG(2) << __func__;
running_ = true; running_ = true;
thread_ = std::thread(&AsyncCallback<Data>::Run, this); thread_ = std::thread(&AsyncCallback<Data>::Run, this);
@ -51,8 +51,10 @@ AsyncCallback<Data>::~AsyncCallback() {
template <class Data> template <class Data>
void AsyncCallback<Data>::PushData(Data data) { void AsyncCallback<Data>::PushData(Data data) {
std::lock_guard<std::mutex> _(mtx_); std::lock_guard<std::mutex> _(mtx_);
if (!concat_) { if (max_data_size_ <= 0) {
datas_.clear(); datas_.clear();
} else if (max_data_size_ == datas_.size()) { // >= 1
datas_.erase(datas_.begin());
} }
datas_.push_back(data); datas_.push_back(data);
++count_; ++count_;