NDEVR
API Documentation
BasicThread

A thread that executes a user-provided callback function concurrently. More...

Inheritance diagram for BasicThread:
[legend]
Collaboration diagram for BasicThread:
[legend]

Public Member Functions

 BasicThread (const StringView &name, const bool &is_loop, const std::function< void()> &callback)
 Constructs a BasicThread with a name, loop flag, and callback function.
void run () override
 Executes the stored callback function.
Public Member Functions inherited from Thread
 Thread ()
 Constructs a Thread with a default name.
 Thread (const StringView &name)
 Constructs a Thread with the specified name.
virtual ~Thread ()
 Destroys the Thread and cleans up resources.
void detach ()
 Detaches the thread, allowing it to run independently.
void forceStop ()
 Forces the running flag to false without waiting for the thread to finish.
bool isPrimaryThread () const
 Checks whether this thread is the primary thread.
bool isRunning () const
 Checks whether the thread is currently running.
void join ()
 Blocks the calling thread until this thread finishes execution.
virtual void requestStopThread ()
 Requests the thread to stop without blocking.
void setIsPrimaryThread (bool is_primary_thread)
 Sets whether this thread should be treated as the primary thread.
void setThreadName (const StringView &name)
 Sets the name of this thread.
virtual bool shouldExitThread () const
 Checks whether the thread has been requested to exit.
void start ()
 Starts the thread execution.
virtual void stopThread ()
 Stops the thread and waits for it to finish.
const StringthreadName () const
 Gets the name assigned to this thread.

Static Public Member Functions

static void splitThread (const std::function< void()> &callback_a, const std::function< void()> &callback_b)
 Splits execution into two concurrent threads and waits for both to complete.
static void splitThread (const StringView &split_name, const std::function< void()> &callback_a, const std::function< void()> &callback_b)
 Splits execution into two named concurrent threads and waits for both to complete.
template<uint04 t_max_number_of_threads>
static std::enable_if< t_max_number_of_threads<=32, void >::type ThreadLoop (uint04 number_of_loops, const std::function< void(uint04 index)> &callback)
 Distributes a loop across multiple threads up to t_max_number_of_threads.
template<uint04 t_max_number_of_threads>
static void ThreadLoop (uint04 number_of_loops, volatile uint04 &loops_performed, const std::function< void(uint04 index)> &callback)
 Executes a loop distributed across a fixed number of threads using recursive splitting.
Static Public Member Functions inherited from Thread
static ThreadCurrentThread ()
 Gets a reference to the Thread object for the calling thread.
static String CurrentThreadName ()
 Gets the name of the current thread.
static void ServiceVirtualThreads ()
 Services any virtual threads that require periodic execution.
static void SetCurrentThreadName (const StringView &name)
 Sets the name of the current thread.
static void StopAllThreads ()
 Stops all tracked threads.

Protected Attributes

std::function< void()> m_function
 The callback function executed by this thread.
Protected Attributes inherited from Thread
volatile bool m_is_running = false
 Whether the thread is currently running.
volatile bool m_is_single_run = true
 Whether the thread should execute only once before stopping.

Additional Inherited Members

Protected Member Functions inherited from Thread
const std::thread & self ()
 Gets a const reference to the underlying std::thread.

Detailed Description

A thread that executes a user-provided callback function concurrently.


See also
Thread, ThreadQueue

Definition at line 46 of file BasicThread.h.

Constructor & Destructor Documentation

◆ BasicThread()

BasicThread::BasicThread ( const StringView & name,
const bool & is_loop,
const std::function< void()> & callback )
explicit

Constructs a BasicThread with a name, loop flag, and callback function.

Parameters
[in]nameThe name identifier for this thread.
[in]is_loopWhether the thread should loop continuously or run once.
[in]callbackThe function to execute on this thread.

Member Function Documentation

◆ run()

void BasicThread::run ( )
inlineoverridevirtual

Executes the stored callback function.

Called when the thread is started.

Reimplemented from Thread.

Definition at line 60 of file BasicThread.h.

References m_function.

◆ splitThread() [1/2]

void BasicThread::splitThread ( const std::function< void()> & callback_a,
const std::function< void()> & callback_b )
static

Splits execution into two concurrent threads and waits for both to complete.

Parameters
[in]callback_aThe function to execute on the first thread.
[in]callback_bThe function to execute on the second thread.

Referenced by ThreadLoop().

◆ splitThread() [2/2]

void BasicThread::splitThread ( const StringView & split_name,
const std::function< void()> & callback_a,
const std::function< void()> & callback_b )
static

Splits execution into two named concurrent threads and waits for both to complete.

Parameters
[in]split_nameThe name prefix for the spawned threads.
[in]callback_aThe function to execute on the first thread.
[in]callback_bThe function to execute on the second thread.

◆ ThreadLoop() [1/2]

template<uint04 t_max_number_of_threads>
std::enable_if< t_max_number_of_threads<=32, void >::type BasicThread::ThreadLoop ( uint04 number_of_loops,
const std::function< void(uint04 index)> & callback )
inlinestatic

Distributes a loop across multiple threads up to t_max_number_of_threads.

Selects the optimal thread count based on the number of loops and available OS threads, then delegates to the fixed-thread-count overload.

Parameters
[in]number_of_loopsThe total number of loop iterations to execute.
[in]callbackThe function to call for each loop index.

Definition at line 87 of file BasicThread.h.

References getMin(), and ThreadLoop().

Referenced by ThreadLoop(), and ThreadLoop().

◆ ThreadLoop() [2/2]

template<uint04 t_max_number_of_threads>
void BasicThread::ThreadLoop ( uint04 number_of_loops,
volatile uint04 & loops_performed,
const std::function< void(uint04 index)> & callback )
inlinestatic

Executes a loop distributed across a fixed number of threads using recursive splitting.

When t_max_number_of_threads is 1, runs iterations sequentially with atomic index incrementing. Otherwise, recursively splits the work across threads.

Parameters
[in]number_of_loopsThe total number of loop iterations to execute.
[in]loops_performedA shared volatile counter tracking completed iterations.
[in]callbackThe function to call for each loop index.

Definition at line 123 of file BasicThread.h.

References ConcurrentOperation::increment(), splitThread(), and ThreadLoop().


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