Skip to content

Register async callback for updates #9

Description

@tylerjw

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions