libstp
Robotics library for Botball
Loading...
Searching...
No Matches
libstp::threading::ThreadManager Class Reference

#include <thread_manager.hpp>

Classes

class  DaemonHandle

Public Member Functions

DaemonHandle add_daemon (std::string name, DaemonFn fn)
void add_persistent_daemon (std::string name, DaemonFn fn)
void shutdown ()
std::size_t daemon_count () const
 Number of currently-registered daemons. Test helper.
 ~ThreadManager ()
 ThreadManager (const ThreadManager &)=delete
ThreadManager & operator= (const ThreadManager &)=delete

Static Public Member Functions

static ThreadManager & instance ()

Friends

class DaemonHandle

Detailed Description

Process-wide registry for background threads.

Every long-lived worker in raccoon-lib goes through this singleton — nothing constructs a bare std::thread of its own. There are two lifetimes:

  • Persistent daemons (add_persistent_daemon) live until the library is unloaded; they are joined by shutdown() (called from the singleton destructor). Use this for global services like the STM32 heartbeat.
  • Scoped daemons (add_daemon) return an RAII DaemonHandle. Dropping the handle requests stop on the underlying std::jthread and joins it. Use this for per-instance workers (e.g. the Localization tick loop), where the thread's lifetime is tied to a particular owning object instead of the process.

Both flavours observe the same std::stop_token contract — daemons must return when the token is requested.

Definition at line 35 of file thread_manager.hpp.

Constructor & Destructor Documentation

◆ ~ThreadManager()

libstp::threading::ThreadManager::~ThreadManager ( )

◆ ThreadManager()

libstp::threading::ThreadManager::ThreadManager ( const ThreadManager & )
delete

Member Function Documentation

◆ add_daemon()

DaemonHandle libstp::threading::ThreadManager::add_daemon ( std::string name,
DaemonFn fn )
nodiscard

Register a scoped daemon. The caller owns the returned handle; dropping it stops and joins the thread. Calling this after shutdown() returns an empty handle and logs a warning.

◆ add_persistent_daemon()

void libstp::threading::ThreadManager::add_persistent_daemon ( std::string name,
DaemonFn fn )

Register a process-lifetime daemon. Joined at shutdown() time. Use this only when the thread must live as long as the library itself does — otherwise prefer add_daemon.

◆ daemon_count()

std::size_t libstp::threading::ThreadManager::daemon_count ( ) const
nodiscard

Number of currently-registered daemons. Test helper.

◆ instance()

ThreadManager & libstp::threading::ThreadManager::instance ( )
static

◆ operator=()

ThreadManager & libstp::threading::ThreadManager::operator= ( const ThreadManager & )
delete

◆ shutdown()

void libstp::threading::ThreadManager::shutdown ( )

Request stop on every registered daemon (persistent and scoped) and join them. Idempotent; safe to call from the singleton destructor and from teardown helpers in tests.

◆ DaemonHandle

friend class DaemonHandle
friend

Definition at line 65 of file thread_manager.hpp.


The documentation for this class was generated from the following file:
  • /home/runner/work/raccoon-lib/raccoon-lib/modules/libstp-threading/include/threading/thread_manager.hpp