Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Observer

From Leeroopedia
Revision as of 13:53, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Serving_Observer.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Design Pattern, Concurrency
Last Updated 2026-02-13 00:00 GMT

Overview

A thread-safe observer wrapper around std::function that allows safe destruction even while external code may be invoking the callback.

Description

Observer<Args...> wraps a std::function and provides a Notifier() method that returns a callable which forwards to the underlying function. The key safety property is that destroying the Observer causes all existing notifiers to become no-ops rather than invoking a dangling callback. This is implemented via a shared Impl object (shared between the Observer and all notifiers) that holds the function behind a mutex. When the Observer is destroyed, it calls Orphan() on the Impl, which sets the function to nullptr under the lock. Subsequent notifications check for nullptr before invoking. ObserverList is a companion class that manages a collection of observers, with automatic garbage collection of orphaned notifier slots when new observers are added. Notifications are serialized through the mutex, so this should not be used for performance-critical long-running callbacks.

Usage

Use this when you need to pass a callback to asynchronous code and want to ensure safe cleanup even if notifications arrive after the callback owner has been destroyed, such as in source or loader notifications in the serving pipeline.

Code Reference

Source Location

Signature

template <typename... Args>
class Observer {
 public:
  using Function = std::function<void(Args...)>;
  explicit Observer(Function f);
  ~Observer();
  Function Notifier() const;
};

template <typename... Args>
class ObserverList {
 public:
  void Add(const Observer<Args...>& new_observer);
  void Notify(Args... args);
  void Clear();
};

Import

#include "tensorflow_serving/util/observer.h"

I/O Contract

Inputs

Name Type Required Description
f std::function<void(Args...)> Yes The function to be called on each notification

Outputs

Name Type Description
Notifier() std::function<void(Args...)> A safe callable that forwards to the observer's function or is a no-op if orphaned

Usage Examples

Safe Asynchronous Notifications

void ObserveSomeNotifications() {
  mutex mu;
  int num_notifications = 0;
  Observer<> observer([&]() { mutex_lock l(mu); ++num_notifications; });
  AsynchronouslySendNotificationsForAWhile(observer.Notifier());
  SleepForALittleWhile();
  {
    mutex_lock l(mu);
    LOG(INFO) << "Currently " << num_notifications << " notifications";
  }
  // Let 'observer' fall out of scope. Future notifications become no-ops.
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment