CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::Runnable Class Reference

Minimal cooperative-shutdown base class for objects that run a worker thread. More...

#include <ThreadManager.h>

Inheritance diagram for cmlabs::Runnable:
[legend]

Public Member Functions

 Runnable ()
 Initialise flags: not running, allowed to continue.
virtual ~Runnable ()
 Destructor requests a stop (with the default timeout) before destruction proceeds.
virtual bool stop (uint32 timeout=200)
 Ask the worker loop to finish and wait for it to do so.

Protected Attributes

uint32 threadID
 ThreadManager slot ID of the worker thread (0 until known).
bool shouldContinue
 Loop-continuation flag; cleared by stop().
bool isRunning
 Set by the worker while its loop is active.

Detailed Description

Minimal cooperative-shutdown base class for objects that run a worker thread.

A subclass runs its loop while shouldContinue is true and sets isRunning around the loop body. stop() flips shouldContinue and then spin-waits (5 ms steps) for the loop to observe it and clear isRunning. This is cooperative: the thread is never forcibly killed by stop(); a loop that blocks indefinitely will simply cause stop() to time out.

Warning
The flags are plain bools with no atomics or memory barriers; the pattern relies on the platforms' strong-enough visibility for single-writer boolean flags. Do not extend it to carry data.

Definition at line 153 of file ThreadManager.h.

Constructor & Destructor Documentation

◆ Runnable()

cmlabs::Runnable::Runnable ( )
inline

Initialise flags: not running, allowed to continue.

Definition at line 156 of file ThreadManager.h.

References isRunning, shouldContinue, and threadID.

◆ ~Runnable()

virtual cmlabs::Runnable::~Runnable ( )
inlinevirtual

Destructor requests a stop (with the default timeout) before destruction proceeds.

Definition at line 158 of file ThreadManager.h.

References stop().

Member Function Documentation

◆ stop()

virtual bool cmlabs::Runnable::stop ( uint32 timeout = 200)
inlinevirtual

Ask the worker loop to finish and wait for it to do so.

Parameters
timeoutMaximum time to wait, in milliseconds (default 200).
Returns
true if the loop ended within the timeout; false if it was still running when time ran out (a diagnostic is logged).
Note
Safe to call multiple times and from any thread.

Definition at line 166 of file ThreadManager.h.

References isRunning, LOG_SYSTEM, LogPrint, shouldContinue, cmlabs::utils::Sleep(), and threadID.

Referenced by cmlabs::NetworkConnection::disconnectInternal(), cmlabs::PsySpace::shutdown(), cmlabs::ThreadManager::shutdown(), cmlabs::RequestClient::~RequestClient(), cmlabs::RequestExecutor::~RequestExecutor(), cmlabs::RequestGateway::~RequestGateway(), and ~Runnable().

Member Data Documentation

◆ isRunning

◆ shouldContinue

◆ threadID


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