Fray Engine
Loading...
Searching...
No Matches
fray::threads::ThreadManager Class Reference

Registry of the engine's threads: starts them, names them, asks them to stop and joins them. Every method is safe from any thread; one internal mutex guards the registry and is never held while joining or running thread bodies. The destructor logs the names of threads still running, requests stop on all, and joins them (blocking). Not copyable. More...

#include <thread_manager.hpp>

Classes

struct  Entry

Public Types

using Id = std::uint64_t
 Handle for a spawned thread; ids start at 1 and are never reused.

Public Member Functions

 ThreadManager ()=default
 Creates an empty manager.
 ThreadManager (const ThreadManager &)=delete
 Not copyable: it owns threads.
ThreadManager & operator= (const ThreadManager &)=delete
 Not copy-assignable: it owns threads.
 ~ThreadManager ()
 Warns about threads still running, requests stop on all of them, then joins them all (blocks until they end).
Id spawn (const std::string &name, std::function< void(StopToken)> body)
 Starts a new thread running body and registers it. Exceptions escaping the body are logged, not propagated.
bool running (Id id) const
 Whether a specific thread is still running.
std::size_t running () const
 Number of registered threads whose body has not yet returned.
std::vector< std::string > runningNames () const
 Names of the threads that are still running, to show what is holding up a shutdown.
void requestStop (Id id)
 Asks one thread to stop by triggering its StopToken. Does not wait; the body must poll the token.
bool join (Id id)
 Blocks until the thread ends, then forgets it. Joining the calling thread is refused. The registry mutex is released before blocking.
void stopAll ()
 Requests stop on every registered thread's token. Does not wait.
void joinAll ()
 Joins every registered thread (blocks until all have ended); a thread cannot join itself, so call from outside the managed threads.

Private Attributes

std::mutex _mutex
std::map< Id, Entry > _entries
Id _next = 1

Detailed Description

Registry of the engine's threads: starts them, names them, asks them to stop and joins them. Every method is safe from any thread; one internal mutex guards the registry and is never held while joining or running thread bodies. The destructor logs the names of threads still running, requests stop on all, and joins them (blocking). Not copyable.

Definition at line 166 of file thread_manager.hpp.

Member Typedef Documentation

◆ Id

using fray::threads::ThreadManager::Id = std::uint64_t

Handle for a spawned thread; ids start at 1 and are never reused.

Definition at line 170 of file thread_manager.hpp.

Constructor & Destructor Documentation

◆ ThreadManager() [1/2]

fray::threads::ThreadManager::ThreadManager ( )
default

Creates an empty manager.

◆ ThreadManager() [2/2]

fray::threads::ThreadManager::ThreadManager ( const ThreadManager & )
delete

Not copyable: it owns threads.

◆ ~ThreadManager()

fray::threads::ThreadManager::~ThreadManager ( )
inline

Warns about threads still running, requests stop on all of them, then joins them all (blocks until they end).

Definition at line 203 of file thread_manager.hpp.

Member Function Documentation

◆ join()

bool fray::threads::ThreadManager::join ( Id id)
inline

Blocks until the thread ends, then forgets it. Joining the calling thread is refused. The registry mutex is released before blocking.

Parameters
idId from spawn().
Returns
false if id is the calling thread itself (nothing done); true otherwise, including when the id is unknown or already joined.

Definition at line 306 of file thread_manager.hpp.

◆ joinAll()

void fray::threads::ThreadManager::joinAll ( )
inline

Joins every registered thread (blocks until all have ended); a thread cannot join itself, so call from outside the managed threads.

Definition at line 334 of file thread_manager.hpp.

◆ operator=()

ThreadManager & fray::threads::ThreadManager::operator= ( const ThreadManager & )
delete

Not copy-assignable: it owns threads.

◆ requestStop()

void fray::threads::ThreadManager::requestStop ( Id id)
inline

Asks one thread to stop by triggering its StopToken. Does not wait; the body must poll the token.

Parameters
idId from spawn(); unknown ids are ignored.

Definition at line 291 of file thread_manager.hpp.

◆ running() [1/2]

std::size_t fray::threads::ThreadManager::running ( ) const
inline

Number of registered threads whose body has not yet returned.

Definition at line 266 of file thread_manager.hpp.

◆ running() [2/2]

bool fray::threads::ThreadManager::running ( Id id) const
inline

Whether a specific thread is still running.

Parameters
idId from spawn().
Returns
true if it exists and its body has not returned; false if finished, joined or unknown.

Definition at line 257 of file thread_manager.hpp.

◆ runningNames()

std::vector< std::string > fray::threads::ThreadManager::runningNames ( ) const
inline

Names of the threads that are still running, to show what is holding up a shutdown.

Definition at line 277 of file thread_manager.hpp.

◆ spawn()

Id fray::threads::ThreadManager::spawn ( const std::string & name,
std::function< void(StopToken)> body )
inline

Starts a new thread running body and registers it. Exceptions escaping the body are logged, not propagated.

Parameters
nameLabel used in logs and runningNames().
bodyFunction run on the new thread; receives the token it should poll to know when to stop.
Returns
Id to use with running(), requestStop() and join().

Definition at line 224 of file thread_manager.hpp.

◆ stopAll()

void fray::threads::ThreadManager::stopAll ( )
inline

Requests stop on every registered thread's token. Does not wait.

Definition at line 326 of file thread_manager.hpp.

Member Data Documentation

◆ _entries

std::map<Id, Entry> fray::threads::ThreadManager::_entries
private

All threads not yet joined.

Definition at line 188 of file thread_manager.hpp.

◆ _mutex

std::mutex fray::threads::ThreadManager::_mutex
mutableprivate

Guards _entries and _next.

Definition at line 186 of file thread_manager.hpp.

◆ _next

Id fray::threads::ThreadManager::_next = 1
private

Next id to hand out.

Definition at line 190 of file thread_manager.hpp.


The documentation for this class was generated from the following file: