Open
Description
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.