Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register async callback for updates #9

Open
tylerjw opened this issue Jul 7, 2022 · 0 comments
Open

Register async callback for updates #9

tylerjw opened this issue Jul 7, 2022 · 0 comments

Comments

@tylerjw
Copy link
Contributor

tylerjw commented Jul 7, 2022

Provide an interface for registering async callbacks for parameters being updated. Be careful that the mutex is not locked when these functions are called so if they call get() it doesn't create a deadlock.

ParameterListener::update()
{
  {
    std::lock_guard lock(mutex_);
    // modify params_ variable
  }
  std::lock_guard lock(update_callbacks_);
  for (auto const& f : update_callbacks_) f(params_);
}

class ParameterListener {
 public:
  void register_callback(std::function<void(Params const&)> cb) {
    std::lock_guard lock(update_callbacks_);
    update_callbacks_.push_back(cb);
  }
 private:
  std::mutex callbacks_mutex_;
  std::vecotr<std::function<void(Params const&)>> update_callbacks_;

Warn users that the lifetime of the callback function has to be at least as long as the lifetime of the ParameterListener object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant